| [b8ab91a] | 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 | // | 
|---|
| [817bb3c] | 7 | // MultiLevelExit.cpp -- Replaces CFA's local control flow with C's versions. | 
|---|
| [b8ab91a] | 8 | // | 
|---|
|  | 9 | // Author           : Andrew Beach | 
|---|
|  | 10 | // Created On       : Mon Nov  1 13:48:00 2021 | 
|---|
| [400b8be] | 11 | // Last Modified By : Andrew Beach | 
|---|
|  | 12 | // Last Modified On : Mon Mar 28  9:42:00 2022 | 
|---|
|  | 13 | // Update Count     : 34 | 
|---|
| [b8ab91a] | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "MultiLevelExit.hpp" | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "AST/Pass.hpp" | 
|---|
|  | 19 | #include "AST/Stmt.hpp" | 
|---|
| [66daee4] | 20 | #include "LabelGeneratorNew.hpp" | 
|---|
| [b8ab91a] | 21 |  | 
|---|
|  | 22 | #include <set> | 
|---|
| [66daee4] | 23 | using namespace std; | 
|---|
|  | 24 | using namespace ast; | 
|---|
| [b8ab91a] | 25 |  | 
|---|
|  | 26 | namespace ControlStruct { | 
|---|
|  | 27 | class Entry { | 
|---|
| [66daee4] | 28 | public: | 
|---|
|  | 29 | const Stmt * stmt; | 
|---|
|  | 30 | private: | 
|---|
| [b8ab91a] | 31 | // Organized like a manual ADT. Avoids creating a bunch of dead data. | 
|---|
|  | 32 | struct Target { | 
|---|
| [66daee4] | 33 | Label label; | 
|---|
| [b8ab91a] | 34 | bool used = false; | 
|---|
| [66daee4] | 35 | Target( const Label & label ) : label( label ) {} | 
|---|
| [b8ab91a] | 36 | Target() : label( CodeLocation() ) {} | 
|---|
|  | 37 | }; | 
|---|
|  | 38 | Target firstTarget; | 
|---|
|  | 39 | Target secondTarget; | 
|---|
|  | 40 |  | 
|---|
|  | 41 | enum Kind { | 
|---|
| [400b8be] | 42 | ForStmtK, WhileDoStmtK, CompoundStmtK, IfStmtK, CaseClauseK, SwitchStmtK, TryStmtK | 
|---|
| [b8ab91a] | 43 | } kind; | 
|---|
|  | 44 |  | 
|---|
|  | 45 | bool fallDefaultValid = true; | 
|---|
|  | 46 |  | 
|---|
| [66daee4] | 47 | static Label & useTarget( Target & target ) { | 
|---|
| [b8ab91a] | 48 | target.used = true; | 
|---|
|  | 49 | return target.label; | 
|---|
|  | 50 | } | 
|---|
| [66daee4] | 51 | public: | 
|---|
|  | 52 | Entry( const ForStmt * stmt, Label breakExit, Label contExit ) : | 
|---|
|  | 53 | stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( ForStmtK ) {} | 
|---|
| [3b0bc16] | 54 | Entry( const WhileDoStmt * stmt, Label breakExit, Label contExit ) : | 
|---|
|  | 55 | stmt( stmt ), firstTarget( breakExit ), secondTarget( contExit ), kind( WhileDoStmtK ) {} | 
|---|
| [66daee4] | 56 | Entry( const CompoundStmt *stmt, Label breakExit ) : | 
|---|
|  | 57 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( CompoundStmtK ) {} | 
|---|
|  | 58 | Entry( const IfStmt *stmt, Label breakExit ) : | 
|---|
|  | 59 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( IfStmtK ) {} | 
|---|
| [400b8be] | 60 | Entry( const CaseClause *, const CompoundStmt *stmt, Label fallExit ) : | 
|---|
|  | 61 | stmt( stmt ), firstTarget( fallExit ), secondTarget(), kind( CaseClauseK ) {} | 
|---|
| [66daee4] | 62 | Entry( const SwitchStmt *stmt, Label breakExit, Label fallDefaultExit ) : | 
|---|
|  | 63 | stmt( stmt ), firstTarget( breakExit ), secondTarget( fallDefaultExit ), kind( SwitchStmtK ) {} | 
|---|
|  | 64 | Entry( const TryStmt *stmt, Label breakExit ) : | 
|---|
|  | 65 | stmt( stmt ), firstTarget( breakExit ), secondTarget(), kind( TryStmtK ) {} | 
|---|
|  | 66 |  | 
|---|
| [3b0bc16] | 67 | bool isContTarget() const { return kind <= WhileDoStmtK; } | 
|---|
| [400b8be] | 68 | bool isBreakTarget() const { return kind != CaseClauseK; } | 
|---|
|  | 69 | bool isFallTarget() const { return kind == CaseClauseK; } | 
|---|
| [66daee4] | 70 | bool isFallDefaultTarget() const { return kind == SwitchStmtK; } | 
|---|
| [b8ab91a] | 71 |  | 
|---|
| [3e5db5b4] | 72 | // These routines set a target as being "used" by a BranchStmt | 
|---|
| [3b0bc16] | 73 | Label useContExit() { assert( kind <= WhileDoStmtK ); return useTarget(secondTarget); } | 
|---|
| [400b8be] | 74 | Label useBreakExit() { assert( kind != CaseClauseK ); return useTarget(firstTarget); } | 
|---|
|  | 75 | Label useFallExit() { assert( kind == CaseClauseK );  return useTarget(firstTarget); } | 
|---|
| [66daee4] | 76 | Label useFallDefaultExit() { assert( kind == SwitchStmtK ); return useTarget(secondTarget); } | 
|---|
| [b8ab91a] | 77 |  | 
|---|
| [3e5db5b4] | 78 | // These routines check if a specific label for a statement is used by a BranchStmt | 
|---|
| [3b0bc16] | 79 | bool isContUsed() const { assert( kind <= WhileDoStmtK ); return secondTarget.used; } | 
|---|
| [400b8be] | 80 | bool isBreakUsed() const { assert( kind != CaseClauseK ); return firstTarget.used; } | 
|---|
|  | 81 | bool isFallUsed() const { assert( kind == CaseClauseK ); return firstTarget.used; } | 
|---|
| [66daee4] | 82 | bool isFallDefaultUsed() const { assert( kind == SwitchStmtK ); return secondTarget.used; } | 
|---|
| [b8ab91a] | 83 | void seenDefault() { fallDefaultValid = false; } | 
|---|
|  | 84 | bool isFallDefaultValid() const { return fallDefaultValid; } | 
|---|
|  | 85 | }; | 
|---|
|  | 86 |  | 
|---|
| [66daee4] | 87 | // Helper predicates used in find_if calls (it doesn't take methods): | 
|---|
| [b8ab91a] | 88 | bool isBreakTarget( const Entry & entry ) { | 
|---|
|  | 89 | return entry.isBreakTarget(); | 
|---|
|  | 90 | } | 
|---|
|  | 91 |  | 
|---|
|  | 92 | bool isContinueTarget( const Entry & entry ) { | 
|---|
|  | 93 | return entry.isContTarget(); | 
|---|
|  | 94 | } | 
|---|
|  | 95 |  | 
|---|
|  | 96 | bool isFallthroughTarget( const Entry & entry ) { | 
|---|
|  | 97 | return entry.isFallTarget(); | 
|---|
|  | 98 | } | 
|---|
|  | 99 |  | 
|---|
|  | 100 | bool isFallthroughDefaultTarget( const Entry & entry ) { | 
|---|
|  | 101 | return entry.isFallDefaultTarget(); | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
| [817bb3c] | 104 | struct MultiLevelExitCore final : | 
|---|
| [66daee4] | 105 | public WithVisitorRef<MultiLevelExitCore>, | 
|---|
|  | 106 | public WithShortCircuiting, public WithGuards { | 
|---|
| [cb921d4] | 107 | MultiLevelExitCore( const LabelToStmt & lt ); | 
|---|
| [b8ab91a] | 108 |  | 
|---|
| [66daee4] | 109 | void previsit( const FunctionDecl * ); | 
|---|
|  | 110 |  | 
|---|
|  | 111 | const CompoundStmt * previsit( const CompoundStmt * ); | 
|---|
|  | 112 | const BranchStmt * postvisit( const BranchStmt * ); | 
|---|
| [3b0bc16] | 113 | void previsit( const WhileDoStmt * ); | 
|---|
|  | 114 | const WhileDoStmt * postvisit( const WhileDoStmt * ); | 
|---|
| [66daee4] | 115 | void previsit( const ForStmt * ); | 
|---|
|  | 116 | const ForStmt * postvisit( const ForStmt * ); | 
|---|
| [400b8be] | 117 | const CaseClause * previsit( const CaseClause * ); | 
|---|
| [66daee4] | 118 | void previsit( const IfStmt * ); | 
|---|
|  | 119 | const IfStmt * postvisit( const IfStmt * ); | 
|---|
|  | 120 | void previsit( const SwitchStmt * ); | 
|---|
|  | 121 | const SwitchStmt * postvisit( const SwitchStmt * ); | 
|---|
|  | 122 | void previsit( const ReturnStmt * ); | 
|---|
|  | 123 | void previsit( const TryStmt * ); | 
|---|
|  | 124 | void postvisit( const TryStmt * ); | 
|---|
| [400b8be] | 125 | void previsit( const FinallyClause * ); | 
|---|
| [66daee4] | 126 |  | 
|---|
|  | 127 | const Stmt * mutateLoop( const Stmt * body, Entry& ); | 
|---|
| [b8ab91a] | 128 |  | 
|---|
| [817bb3c] | 129 | const LabelToStmt & target_table; | 
|---|
| [66daee4] | 130 | set<Label> fallthrough_labels; | 
|---|
|  | 131 | vector<Entry> enclosing_control_structures; | 
|---|
|  | 132 | Label break_label; | 
|---|
| [b8ab91a] | 133 | bool inFinally; | 
|---|
|  | 134 |  | 
|---|
|  | 135 | template<typename LoopNode> | 
|---|
|  | 136 | void prehandleLoopStmt( const LoopNode * loopStmt ); | 
|---|
|  | 137 | template<typename LoopNode> | 
|---|
|  | 138 | const LoopNode * posthandleLoopStmt( const LoopNode * loopStmt ); | 
|---|
|  | 139 |  | 
|---|
| [66daee4] | 140 | list<ptr<Stmt>> fixBlock( | 
|---|
|  | 141 | const list<ptr<Stmt>> & kids, bool caseClause ); | 
|---|
| [b8ab91a] | 142 |  | 
|---|
| [817bb3c] | 143 | template<typename UnaryPredicate> | 
|---|
|  | 144 | auto findEnclosingControlStructure( UnaryPredicate pred ) { | 
|---|
| [66daee4] | 145 | return find_if( enclosing_control_structures.rbegin(), | 
|---|
|  | 146 | enclosing_control_structures.rend(), pred ); | 
|---|
| [817bb3c] | 147 | } | 
|---|
|  | 148 | }; | 
|---|
| [b8ab91a] | 149 |  | 
|---|
| [66daee4] | 150 | NullStmt * labelledNullStmt( | 
|---|
|  | 151 | const CodeLocation & cl, const Label & label ) { | 
|---|
|  | 152 | return new NullStmt( cl, vector<Label>{ label } ); | 
|---|
| [b8ab91a] | 153 | } | 
|---|
|  | 154 |  | 
|---|
| [cb921d4] | 155 | MultiLevelExitCore::MultiLevelExitCore( const LabelToStmt & lt ) : | 
|---|
|  | 156 | target_table( lt ), break_label( CodeLocation(), "" ), | 
|---|
| [b8ab91a] | 157 | inFinally( false ) | 
|---|
|  | 158 | {} | 
|---|
|  | 159 |  | 
|---|
| [66daee4] | 160 | void MultiLevelExitCore::previsit( const FunctionDecl * ) { | 
|---|
| [b8ab91a] | 161 | visit_children = false; | 
|---|
|  | 162 | } | 
|---|
|  | 163 |  | 
|---|
| [66daee4] | 164 | const CompoundStmt * MultiLevelExitCore::previsit( | 
|---|
|  | 165 | const CompoundStmt * stmt ) { | 
|---|
| [b8ab91a] | 166 | visit_children = false; | 
|---|
| [3e5db5b4] | 167 |  | 
|---|
|  | 168 | // if the stmt is labelled then generate a label to check in postvisit if the label is used | 
|---|
| [2f52b18] | 169 | bool isLabeled = ! stmt->labels.empty(); | 
|---|
| [b8ab91a] | 170 | if ( isLabeled ) { | 
|---|
| [66daee4] | 171 | Label breakLabel = newLabel( "blockBreak", stmt ); | 
|---|
| [b8ab91a] | 172 | enclosing_control_structures.emplace_back( stmt, breakLabel ); | 
|---|
|  | 173 | GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); | 
|---|
|  | 174 | } | 
|---|
|  | 175 |  | 
|---|
| [66daee4] | 176 | auto mutStmt = mutate( stmt ); | 
|---|
| [b8ab91a] | 177 | // A child statement may set the break label. | 
|---|
| [891f707] | 178 | mutStmt->kids = fixBlock( stmt->kids, false ); | 
|---|
| [b8ab91a] | 179 |  | 
|---|
|  | 180 | if ( isLabeled ) { | 
|---|
| [2f52b18] | 181 | assert( ! enclosing_control_structures.empty() ); | 
|---|
| [b8ab91a] | 182 | Entry & entry = enclosing_control_structures.back(); | 
|---|
| [2f52b18] | 183 | if ( ! entry.useBreakExit().empty() ) { | 
|---|
| [b8ab91a] | 184 | break_label = entry.useBreakExit(); | 
|---|
|  | 185 | } | 
|---|
|  | 186 | } | 
|---|
|  | 187 | return mutStmt; | 
|---|
|  | 188 | } | 
|---|
|  | 189 |  | 
|---|
|  | 190 | size_t getUnusedIndex( | 
|---|
| [66daee4] | 191 | const Stmt * stmt, const Label & originalTarget ) { | 
|---|
| [b8ab91a] | 192 | const size_t size = stmt->labels.size(); | 
|---|
|  | 193 |  | 
|---|
| [66daee4] | 194 | // If the label is empty, do not add unused attribute. | 
|---|
|  | 195 | if ( originalTarget.empty() ) return size; | 
|---|
| [b8ab91a] | 196 |  | 
|---|
|  | 197 | // Search for a label that matches the originalTarget. | 
|---|
|  | 198 | for ( size_t i = 0 ; i < size ; ++i ) { | 
|---|
| [66daee4] | 199 | const Label & label = stmt->labels[i]; | 
|---|
| [b8ab91a] | 200 | if ( label == originalTarget ) { | 
|---|
| [66daee4] | 201 | for ( const Attribute * attr : label.attributes ) { | 
|---|
| [b8ab91a] | 202 | if ( attr->name == "unused" ) return size; | 
|---|
|  | 203 | } | 
|---|
|  | 204 | return i; | 
|---|
|  | 205 | } | 
|---|
|  | 206 | } | 
|---|
| [6180274] | 207 | assertf( false, "CFA internal error: could not find label '%s' on statement %s", | 
|---|
| [66daee4] | 208 | originalTarget.name.c_str(), toString( stmt ).c_str() ); | 
|---|
| [b8ab91a] | 209 | } | 
|---|
|  | 210 |  | 
|---|
| [66daee4] | 211 | const Stmt * addUnused( | 
|---|
|  | 212 | const Stmt * stmt, const Label & originalTarget ) { | 
|---|
| [b8ab91a] | 213 | size_t i = getUnusedIndex( stmt, originalTarget ); | 
|---|
|  | 214 | if ( i == stmt->labels.size() ) { | 
|---|
|  | 215 | return stmt; | 
|---|
|  | 216 | } | 
|---|
| [66daee4] | 217 | Stmt * mutStmt = mutate( stmt ); | 
|---|
|  | 218 | mutStmt->labels[i].attributes.push_back( new Attribute( "unused" ) ); | 
|---|
| [b8ab91a] | 219 | return mutStmt; | 
|---|
|  | 220 | } | 
|---|
|  | 221 |  | 
|---|
| [3e5db5b4] | 222 | // This routine updates targets on enclosing control structures to indicate which | 
|---|
|  | 223 | //     label is used by the BranchStmt that is passed | 
|---|
| [66daee4] | 224 | const BranchStmt * MultiLevelExitCore::postvisit( const BranchStmt * stmt ) { | 
|---|
|  | 225 | vector<Entry>::reverse_iterator targetEntry = | 
|---|
| [b8ab91a] | 226 | enclosing_control_structures.rend(); | 
|---|
| [3e5db5b4] | 227 |  | 
|---|
|  | 228 | // Labels on different stmts require different approaches to access | 
|---|
| [b8ab91a] | 229 | switch ( stmt->kind ) { | 
|---|
| [66daee4] | 230 | case BranchStmt::Goto: | 
|---|
| [b8ab91a] | 231 | return stmt; | 
|---|
| [66daee4] | 232 | case BranchStmt::Continue: | 
|---|
|  | 233 | case BranchStmt::Break: { | 
|---|
|  | 234 | bool isContinue = stmt->kind == BranchStmt::Continue; | 
|---|
|  | 235 | // Handle unlabeled break and continue. | 
|---|
|  | 236 | if ( stmt->target.empty() ) { | 
|---|
|  | 237 | if ( isContinue ) { | 
|---|
|  | 238 | targetEntry = findEnclosingControlStructure( isContinueTarget ); | 
|---|
|  | 239 | } else { | 
|---|
|  | 240 | if ( enclosing_control_structures.empty() ) { | 
|---|
|  | 241 | SemanticError( stmt->location, | 
|---|
|  | 242 | "'break' outside a loop, 'switch', or labelled block" ); | 
|---|
|  | 243 | } | 
|---|
|  | 244 | targetEntry = findEnclosingControlStructure( isBreakTarget ); | 
|---|
|  | 245 | } | 
|---|
|  | 246 | // Handle labeled break and continue. | 
|---|
|  | 247 | } else { | 
|---|
|  | 248 | // Lookup label in table to find attached control structure. | 
|---|
|  | 249 | targetEntry = findEnclosingControlStructure( | 
|---|
|  | 250 | [ targetStmt = target_table.at(stmt->target) ](auto entry){ | 
|---|
|  | 251 | return entry.stmt == targetStmt; | 
|---|
|  | 252 | } ); | 
|---|
|  | 253 | } | 
|---|
|  | 254 | // Ensure that selected target is valid. | 
|---|
| [2f52b18] | 255 | if ( targetEntry == enclosing_control_structures.rend() || ( isContinue && ! isContinueTarget( *targetEntry ) ) ) { | 
|---|
| [66daee4] | 256 | SemanticError( stmt->location, toString( (isContinue ? "'continue'" : "'break'"), | 
|---|
|  | 257 | " target must be an enclosing ", (isContinue ? "loop: " : "control structure: "), | 
|---|
|  | 258 | stmt->originalTarget ) ); | 
|---|
|  | 259 | } | 
|---|
|  | 260 | break; | 
|---|
|  | 261 | } | 
|---|
| [34c32f0] | 262 | // handle fallthrough in case/switch stmts | 
|---|
| [66daee4] | 263 | case BranchStmt::FallThrough: { | 
|---|
|  | 264 | targetEntry = findEnclosingControlStructure( isFallthroughTarget ); | 
|---|
|  | 265 | // Check that target is valid. | 
|---|
|  | 266 | if ( targetEntry == enclosing_control_structures.rend() ) { | 
|---|
|  | 267 | SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); | 
|---|
|  | 268 | } | 
|---|
| [2f52b18] | 269 | if ( ! stmt->target.empty() ) { | 
|---|
| [66daee4] | 270 | // Labelled fallthrough: target must be a valid fallthough label. | 
|---|
| [2f52b18] | 271 | if ( ! fallthrough_labels.count( stmt->target ) ) { | 
|---|
| [66daee4] | 272 | SemanticError( stmt->location, toString( "'fallthrough' target must be a later case statement: ", | 
|---|
|  | 273 | stmt->originalTarget ) ); | 
|---|
|  | 274 | } | 
|---|
| [2f52b18] | 275 | return new BranchStmt( stmt->location, BranchStmt::Goto, stmt->originalTarget ); | 
|---|
| [66daee4] | 276 | } | 
|---|
|  | 277 | break; | 
|---|
|  | 278 | } | 
|---|
|  | 279 | case BranchStmt::FallThroughDefault: { | 
|---|
|  | 280 | targetEntry = findEnclosingControlStructure( isFallthroughDefaultTarget ); | 
|---|
|  | 281 |  | 
|---|
|  | 282 | // Check if in switch or choose statement. | 
|---|
|  | 283 | if ( targetEntry == enclosing_control_structures.rend() ) { | 
|---|
|  | 284 | SemanticError( stmt->location, "'fallthrough' must be enclosed in a 'switch' or 'choose'" ); | 
|---|
|  | 285 | } | 
|---|
|  | 286 |  | 
|---|
|  | 287 | // Check if switch or choose has default clause. | 
|---|
|  | 288 | auto switchStmt = strict_dynamic_cast< const SwitchStmt * >( targetEntry->stmt ); | 
|---|
|  | 289 | bool foundDefault = false; | 
|---|
| [400b8be] | 290 | for ( auto caseStmt : switchStmt->cases ) { | 
|---|
| [66daee4] | 291 | if ( caseStmt->isDefault() ) { | 
|---|
|  | 292 | foundDefault = true; | 
|---|
|  | 293 | break; | 
|---|
|  | 294 | } | 
|---|
|  | 295 | } | 
|---|
|  | 296 | if ( ! foundDefault ) { | 
|---|
|  | 297 | SemanticError( stmt->location, "'fallthrough default' must be enclosed in a 'switch' or 'choose'" | 
|---|
|  | 298 | "control structure with a 'default' clause" ); | 
|---|
|  | 299 | } | 
|---|
|  | 300 | break; | 
|---|
|  | 301 | } | 
|---|
|  | 302 | default: | 
|---|
| [b8ab91a] | 303 | assert( false ); | 
|---|
|  | 304 | } | 
|---|
|  | 305 |  | 
|---|
| [2f52b18] | 306 | // Branch error checks: get the appropriate label name, which is always replaced. | 
|---|
| [66daee4] | 307 | Label exitLabel( CodeLocation(), "" ); | 
|---|
| [b8ab91a] | 308 | switch ( stmt->kind ) { | 
|---|
| [66daee4] | 309 | case BranchStmt::Break: | 
|---|
| [2f52b18] | 310 | assert( ! targetEntry->useBreakExit().empty() ); | 
|---|
| [b8ab91a] | 311 | exitLabel = targetEntry->useBreakExit(); | 
|---|
|  | 312 | break; | 
|---|
| [66daee4] | 313 | case BranchStmt::Continue: | 
|---|
| [2f52b18] | 314 | assert( ! targetEntry->useContExit().empty() ); | 
|---|
| [b8ab91a] | 315 | exitLabel = targetEntry->useContExit(); | 
|---|
|  | 316 | break; | 
|---|
| [66daee4] | 317 | case BranchStmt::FallThrough: | 
|---|
| [2f52b18] | 318 | assert( ! targetEntry->useFallExit().empty() ); | 
|---|
| [b8ab91a] | 319 | exitLabel = targetEntry->useFallExit(); | 
|---|
|  | 320 | break; | 
|---|
| [66daee4] | 321 | case BranchStmt::FallThroughDefault: | 
|---|
| [2f52b18] | 322 | assert( ! targetEntry->useFallDefaultExit().empty() ); | 
|---|
| [b8ab91a] | 323 | exitLabel = targetEntry->useFallDefaultExit(); | 
|---|
|  | 324 | // Check that fallthrough default comes before the default clause. | 
|---|
| [2f52b18] | 325 | if ( ! targetEntry->isFallDefaultValid() ) { | 
|---|
| [66daee4] | 326 | SemanticError( stmt->location, "'fallthrough default' must precede the 'default' clause" ); | 
|---|
| [b8ab91a] | 327 | } | 
|---|
|  | 328 | break; | 
|---|
| [66daee4] | 329 | default: | 
|---|
| [b8ab91a] | 330 | assert(0); | 
|---|
|  | 331 | } | 
|---|
|  | 332 |  | 
|---|
|  | 333 | // Add unused attribute to silence warnings. | 
|---|
|  | 334 | targetEntry->stmt = addUnused( targetEntry->stmt, stmt->originalTarget ); | 
|---|
|  | 335 |  | 
|---|
| [66daee4] | 336 | // Replace with goto to make later passes more uniform. | 
|---|
|  | 337 | return new BranchStmt( stmt->location, BranchStmt::Goto, exitLabel ); | 
|---|
| [b8ab91a] | 338 | } | 
|---|
|  | 339 |  | 
|---|
| [3b0bc16] | 340 | void MultiLevelExitCore::previsit( const WhileDoStmt * stmt ) { | 
|---|
| [b8ab91a] | 341 | return prehandleLoopStmt( stmt ); | 
|---|
|  | 342 | } | 
|---|
|  | 343 |  | 
|---|
| [3b0bc16] | 344 | const WhileDoStmt * MultiLevelExitCore::postvisit( const WhileDoStmt * stmt ) { | 
|---|
| [b8ab91a] | 345 | return posthandleLoopStmt( stmt ); | 
|---|
|  | 346 | } | 
|---|
|  | 347 |  | 
|---|
| [66daee4] | 348 | void MultiLevelExitCore::previsit( const ForStmt * stmt ) { | 
|---|
| [b8ab91a] | 349 | return prehandleLoopStmt( stmt ); | 
|---|
|  | 350 | } | 
|---|
|  | 351 |  | 
|---|
| [66daee4] | 352 | const ForStmt * MultiLevelExitCore::postvisit( const ForStmt * stmt ) { | 
|---|
| [b8ab91a] | 353 | return posthandleLoopStmt( stmt ); | 
|---|
|  | 354 | } | 
|---|
|  | 355 |  | 
|---|
|  | 356 | // Mimic what the built-in push_front would do anyways. It is O(n). | 
|---|
|  | 357 | void push_front( | 
|---|
| [66daee4] | 358 | vector<ptr<Stmt>> & vec, const Stmt * element ) { | 
|---|
| [b8ab91a] | 359 | vec.emplace_back( nullptr ); | 
|---|
|  | 360 | for ( size_t i = vec.size() - 1 ; 0 < i ; --i ) { | 
|---|
| [66daee4] | 361 | vec[ i ] = move( vec[ i - 1 ] ); | 
|---|
| [b8ab91a] | 362 | } | 
|---|
|  | 363 | vec[ 0 ] = element; | 
|---|
|  | 364 | } | 
|---|
|  | 365 |  | 
|---|
| [400b8be] | 366 | const CaseClause * MultiLevelExitCore::previsit( const CaseClause * stmt ) { | 
|---|
| [b8ab91a] | 367 | visit_children = false; | 
|---|
|  | 368 |  | 
|---|
| [66daee4] | 369 | // If default, mark seen. | 
|---|
| [b8ab91a] | 370 | if ( stmt->isDefault() ) { | 
|---|
| [2f52b18] | 371 | assert( ! enclosing_control_structures.empty() ); | 
|---|
| [b8ab91a] | 372 | enclosing_control_structures.back().seenDefault(); | 
|---|
|  | 373 | } | 
|---|
|  | 374 |  | 
|---|
|  | 375 | // The cond may not exist, but if it does update it now. | 
|---|
| [400b8be] | 376 | visitor->maybe_accept( stmt, &CaseClause::cond ); | 
|---|
| [b8ab91a] | 377 |  | 
|---|
|  | 378 | // Just save the mutated node for simplicity. | 
|---|
| [400b8be] | 379 | CaseClause * mutStmt = mutate( stmt ); | 
|---|
| [b8ab91a] | 380 |  | 
|---|
| [400b8be] | 381 | Label fallLabel = newLabel( "fallThrough", stmt->location ); | 
|---|
| [66daee4] | 382 | if ( ! mutStmt->stmts.empty() ) { | 
|---|
| [400b8be] | 383 | // These should already be in a block. | 
|---|
|  | 384 | auto first = mutStmt->stmts.front().get_and_mutate(); | 
|---|
|  | 385 | auto block = strict_dynamic_cast<CompoundStmt *>( first ); | 
|---|
|  | 386 |  | 
|---|
| [b8ab91a] | 387 | // Ensure that the stack isn't corrupted by exceptions in fixBlock. | 
|---|
|  | 388 | auto guard = makeFuncGuard( | 
|---|
| [400b8be] | 389 | [&](){ enclosing_control_structures.emplace_back( mutStmt, block, fallLabel ); }, | 
|---|
| [b8ab91a] | 390 | [this](){ enclosing_control_structures.pop_back(); } | 
|---|
| [66daee4] | 391 | ); | 
|---|
| [b8ab91a] | 392 |  | 
|---|
|  | 393 | block->kids = fixBlock( block->kids, true ); | 
|---|
|  | 394 |  | 
|---|
|  | 395 | // Add fallthrough label if necessary. | 
|---|
| [66daee4] | 396 | assert( ! enclosing_control_structures.empty() ); | 
|---|
| [b8ab91a] | 397 | Entry & entry = enclosing_control_structures.back(); | 
|---|
|  | 398 | if ( entry.isFallUsed() ) { | 
|---|
| [400b8be] | 399 | mutStmt->stmts.push_back( labelledNullStmt( block->location, entry.useFallExit() ) ); | 
|---|
| [b8ab91a] | 400 | } | 
|---|
|  | 401 | } | 
|---|
| [66daee4] | 402 | assert( ! enclosing_control_structures.empty() ); | 
|---|
| [b8ab91a] | 403 | Entry & entry = enclosing_control_structures.back(); | 
|---|
| [66daee4] | 404 | assertf( dynamic_cast< const SwitchStmt * >( entry.stmt ), | 
|---|
| [6180274] | 405 | "CFA internal error: control structure enclosing a case clause must be a switch, but is: %s", | 
|---|
| [66daee4] | 406 | toString( entry.stmt ).c_str() ); | 
|---|
| [b8ab91a] | 407 | if ( mutStmt->isDefault() ) { | 
|---|
|  | 408 | if ( entry.isFallDefaultUsed() ) { | 
|---|
|  | 409 | // Add fallthrough default label if necessary. | 
|---|
| [2f52b18] | 410 | push_front( mutStmt->stmts, labelledNullStmt( stmt->location, entry.useFallDefaultExit() ) ); | 
|---|
| [b8ab91a] | 411 | } | 
|---|
|  | 412 | } | 
|---|
|  | 413 | return mutStmt; | 
|---|
|  | 414 | } | 
|---|
|  | 415 |  | 
|---|
| [66daee4] | 416 | void MultiLevelExitCore::previsit( const IfStmt * stmt ) { | 
|---|
| [2f52b18] | 417 | bool labeledBlock = ! stmt->labels.empty(); | 
|---|
| [b8ab91a] | 418 | if ( labeledBlock ) { | 
|---|
| [66daee4] | 419 | Label breakLabel = newLabel( "blockBreak", stmt ); | 
|---|
| [b8ab91a] | 420 | enclosing_control_structures.emplace_back( stmt, breakLabel ); | 
|---|
|  | 421 | GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); | 
|---|
|  | 422 | } | 
|---|
|  | 423 | } | 
|---|
|  | 424 |  | 
|---|
| [66daee4] | 425 | const IfStmt * MultiLevelExitCore::postvisit( const IfStmt * stmt ) { | 
|---|
| [2f52b18] | 426 | bool labeledBlock = ! stmt->labels.empty(); | 
|---|
| [b8ab91a] | 427 | if ( labeledBlock ) { | 
|---|
|  | 428 | auto this_label = enclosing_control_structures.back().useBreakExit(); | 
|---|
| [2f52b18] | 429 | if ( ! this_label.empty() ) { | 
|---|
| [b8ab91a] | 430 | break_label = this_label; | 
|---|
|  | 431 | } | 
|---|
|  | 432 | } | 
|---|
|  | 433 | return stmt; | 
|---|
|  | 434 | } | 
|---|
|  | 435 |  | 
|---|
| [400b8be] | 436 | static bool isDefaultCase( const ptr<CaseClause> & caseClause ) { | 
|---|
|  | 437 | return caseClause->isDefault(); | 
|---|
| [b8ab91a] | 438 | } | 
|---|
|  | 439 |  | 
|---|
| [66daee4] | 440 | void MultiLevelExitCore::previsit( const SwitchStmt * stmt ) { | 
|---|
|  | 441 | Label label = newLabel( "switchBreak", stmt ); | 
|---|
| [400b8be] | 442 | auto it = find_if( stmt->cases.rbegin(), stmt->cases.rend(), isDefaultCase ); | 
|---|
| [b8ab91a] | 443 |  | 
|---|
| [400b8be] | 444 | const CaseClause * defaultCase = it != stmt->cases.rend() ? (*it) : nullptr; | 
|---|
|  | 445 | Label defaultLabel = defaultCase ? newLabel( "fallThroughDefault", defaultCase->location ) : Label( stmt->location, "" ); | 
|---|
| [b8ab91a] | 446 | enclosing_control_structures.emplace_back( stmt, label, defaultLabel ); | 
|---|
|  | 447 | GuardAction( [this]() { enclosing_control_structures.pop_back(); } ); | 
|---|
|  | 448 |  | 
|---|
| [2f52b18] | 449 | // Collect valid labels for fallthrough. It starts with all labels at this level, then remove as each is seen during | 
|---|
|  | 450 | // traversal. | 
|---|
| [400b8be] | 451 | for ( const CaseClause * caseStmt : stmt->cases ) { | 
|---|
| [b8ab91a] | 452 | if ( caseStmt->stmts.empty() ) continue; | 
|---|
| [66daee4] | 453 | auto block = caseStmt->stmts.front().strict_as<CompoundStmt>(); | 
|---|
|  | 454 | for ( const Stmt * stmt : block->kids ) { | 
|---|
|  | 455 | for ( const Label & l : stmt->labels ) { | 
|---|
| [b8ab91a] | 456 | fallthrough_labels.insert( l ); | 
|---|
|  | 457 | } | 
|---|
|  | 458 | } | 
|---|
|  | 459 | } | 
|---|
|  | 460 | } | 
|---|
|  | 461 |  | 
|---|
| [66daee4] | 462 | const SwitchStmt * MultiLevelExitCore::postvisit( const SwitchStmt * stmt ) { | 
|---|
| [2f52b18] | 463 | assert( ! enclosing_control_structures.empty() ); | 
|---|
| [b8ab91a] | 464 | Entry & entry = enclosing_control_structures.back(); | 
|---|
|  | 465 | assert( entry.stmt == stmt ); | 
|---|
|  | 466 |  | 
|---|
| [66daee4] | 467 | // Only run to generate the break label. | 
|---|
| [b8ab91a] | 468 | if ( entry.isBreakUsed() ) { | 
|---|
| [2f52b18] | 469 | // To keep the switch statements uniform (all direct children of a SwitchStmt should be CastStmts), append the | 
|---|
|  | 470 | // exit label and break to the last case, create a default case if no cases. | 
|---|
| [66daee4] | 471 | SwitchStmt * mutStmt = mutate( stmt ); | 
|---|
| [400b8be] | 472 | if ( mutStmt->cases.empty() ) { | 
|---|
|  | 473 | mutStmt->cases.push_back( new CaseClause( mutStmt->location, nullptr, {} ) ); | 
|---|
| [b8ab91a] | 474 | } | 
|---|
|  | 475 |  | 
|---|
| [400b8be] | 476 | auto caseStmt = mutStmt->cases.back().get(); | 
|---|
| [66daee4] | 477 | auto mutCase = mutate( caseStmt ); | 
|---|
| [400b8be] | 478 | mutStmt->cases.back() = mutCase; | 
|---|
| [b8ab91a] | 479 |  | 
|---|
| [66daee4] | 480 | Label label( mutCase->location, "breakLabel" ); | 
|---|
|  | 481 | auto branch = new BranchStmt( mutCase->location, BranchStmt::Break, label ); | 
|---|
| [b8ab91a] | 482 | branch->labels.push_back( entry.useBreakExit() ); | 
|---|
|  | 483 | mutCase->stmts.push_back( branch ); | 
|---|
|  | 484 |  | 
|---|
|  | 485 | return mutStmt; | 
|---|
|  | 486 | } | 
|---|
|  | 487 | return stmt; | 
|---|
|  | 488 | } | 
|---|
|  | 489 |  | 
|---|
| [66daee4] | 490 | void MultiLevelExitCore::previsit( const ReturnStmt * stmt ) { | 
|---|
| [b8ab91a] | 491 | if ( inFinally ) { | 
|---|
|  | 492 | SemanticError( stmt->location, "'return' may not appear in a finally clause" ); | 
|---|
|  | 493 | } | 
|---|
|  | 494 | } | 
|---|
|  | 495 |  | 
|---|
| [66daee4] | 496 | void MultiLevelExitCore::previsit( const TryStmt * stmt ) { | 
|---|
| [2f52b18] | 497 | bool isLabeled = ! stmt->labels.empty(); | 
|---|
| [b8ab91a] | 498 | if ( isLabeled ) { | 
|---|
| [66daee4] | 499 | Label breakLabel = newLabel( "blockBreak", stmt ); | 
|---|
| [b8ab91a] | 500 | enclosing_control_structures.emplace_back( stmt, breakLabel ); | 
|---|
|  | 501 | GuardAction([this](){ enclosing_control_structures.pop_back(); } ); | 
|---|
|  | 502 | } | 
|---|
|  | 503 | } | 
|---|
|  | 504 |  | 
|---|
| [66daee4] | 505 | void MultiLevelExitCore::postvisit( const TryStmt * stmt ) { | 
|---|
| [2f52b18] | 506 | bool isLabeled = ! stmt->labels.empty(); | 
|---|
| [b8ab91a] | 507 | if ( isLabeled ) { | 
|---|
|  | 508 | auto this_label = enclosing_control_structures.back().useBreakExit(); | 
|---|
| [2f52b18] | 509 | if ( ! this_label.empty() ) { | 
|---|
| [b8ab91a] | 510 | break_label = this_label; | 
|---|
|  | 511 | } | 
|---|
|  | 512 | } | 
|---|
|  | 513 | } | 
|---|
|  | 514 |  | 
|---|
| [400b8be] | 515 | void MultiLevelExitCore::previsit( const FinallyClause * ) { | 
|---|
| [2f52b18] | 516 | GuardAction([this, old = move( enclosing_control_structures)](){ enclosing_control_structures = move(old); }); | 
|---|
| [66daee4] | 517 | enclosing_control_structures = vector<Entry>(); | 
|---|
| [b8ab91a] | 518 | GuardValue( inFinally ) = true; | 
|---|
|  | 519 | } | 
|---|
|  | 520 |  | 
|---|
| [66daee4] | 521 | const Stmt * MultiLevelExitCore::mutateLoop( | 
|---|
|  | 522 | const Stmt * body, Entry & entry ) { | 
|---|
| [b8ab91a] | 523 | if ( entry.isBreakUsed() ) { | 
|---|
|  | 524 | break_label = entry.useBreakExit(); | 
|---|
|  | 525 | } | 
|---|
|  | 526 |  | 
|---|
| [3e5db5b4] | 527 | // if continue is used insert a continue label into the back of the body of the loop | 
|---|
| [b8ab91a] | 528 | if ( entry.isContUsed() ) { | 
|---|
| [66daee4] | 529 | CompoundStmt * new_body = new CompoundStmt( body->location ); | 
|---|
| [3e5db5b4] | 530 | // {} | 
|---|
| [b8ab91a] | 531 | new_body->kids.push_back( body ); | 
|---|
| [3e5db5b4] | 532 | // { | 
|---|
|  | 533 | //  body | 
|---|
|  | 534 | // } | 
|---|
| [817bb3c] | 535 | new_body->kids.push_back( | 
|---|
|  | 536 | labelledNullStmt( body->location, entry.useContExit() ) ); | 
|---|
| [3e5db5b4] | 537 | // { | 
|---|
|  | 538 | //  body | 
|---|
|  | 539 | //  ContinueLabel: {} | 
|---|
|  | 540 | // } | 
|---|
| [b8ab91a] | 541 | return new_body; | 
|---|
|  | 542 | } | 
|---|
|  | 543 |  | 
|---|
|  | 544 | return body; | 
|---|
|  | 545 | } | 
|---|
|  | 546 |  | 
|---|
|  | 547 | template<typename LoopNode> | 
|---|
|  | 548 | void MultiLevelExitCore::prehandleLoopStmt( const LoopNode * loopStmt ) { | 
|---|
|  | 549 | // Remember is loop before going onto mutate the body. | 
|---|
|  | 550 | // The labels will be folded in if they are used. | 
|---|
| [66daee4] | 551 | Label breakLabel = newLabel( "loopBreak", loopStmt ); | 
|---|
|  | 552 | Label contLabel = newLabel( "loopContinue", loopStmt ); | 
|---|
| [b8ab91a] | 553 | enclosing_control_structures.emplace_back( loopStmt, breakLabel, contLabel ); | 
|---|
| [3e5db5b4] | 554 | // labels are added temporarily to see if they are used and then added permanently in postvisit if ther are used | 
|---|
|  | 555 | // children will tag labels as being used during their traversal which occurs before postvisit | 
|---|
|  | 556 |  | 
|---|
|  | 557 | // GuardAction calls the lambda after the node is done being visited | 
|---|
| [b8ab91a] | 558 | GuardAction( [this](){ enclosing_control_structures.pop_back(); } ); | 
|---|
|  | 559 | } | 
|---|
|  | 560 |  | 
|---|
|  | 561 | template<typename LoopNode> | 
|---|
|  | 562 | const LoopNode * MultiLevelExitCore::posthandleLoopStmt( const LoopNode * loopStmt ) { | 
|---|
| [2f52b18] | 563 | assert( ! enclosing_control_structures.empty() ); | 
|---|
| [b8ab91a] | 564 | Entry & entry = enclosing_control_structures.back(); | 
|---|
|  | 565 | assert( entry.stmt == loopStmt ); | 
|---|
|  | 566 |  | 
|---|
| [66daee4] | 567 | // Now check if the labels are used and add them if so. | 
|---|
| [2f52b18] | 568 | return mutate_field( loopStmt, &LoopNode::body, mutateLoop( loopStmt->body, entry ) ); | 
|---|
| [3e5db5b4] | 569 | // this call to mutate_field compares loopStmt->body and the result of mutateLoop | 
|---|
|  | 570 | //              if they are the same the node isn't mutated, if they differ then the new mutated node is returned | 
|---|
|  | 571 | //              the stmts will only differ if a label is used | 
|---|
| [b8ab91a] | 572 | } | 
|---|
|  | 573 |  | 
|---|
| [66daee4] | 574 | list<ptr<Stmt>> MultiLevelExitCore::fixBlock( | 
|---|
|  | 575 | const list<ptr<Stmt>> & kids, bool is_case_clause ) { | 
|---|
|  | 576 | // Unfortunately cannot use automatic error collection. | 
|---|
| [b8ab91a] | 577 | SemanticErrorException errors; | 
|---|
|  | 578 |  | 
|---|
| [66daee4] | 579 | list<ptr<Stmt>> ret; | 
|---|
| [b8ab91a] | 580 |  | 
|---|
|  | 581 | // Manually visit each child. | 
|---|
| [66daee4] | 582 | for ( const ptr<Stmt> & kid : kids ) { | 
|---|
| [b8ab91a] | 583 | if ( is_case_clause ) { | 
|---|
|  | 584 | // Once a label is seen, it's no longer a valid for fallthrough. | 
|---|
| [66daee4] | 585 | for ( const Label & l : kid->labels ) { | 
|---|
| [b8ab91a] | 586 | fallthrough_labels.erase( l ); | 
|---|
|  | 587 | } | 
|---|
|  | 588 | } | 
|---|
|  | 589 |  | 
|---|
|  | 590 | try { | 
|---|
|  | 591 | ret.push_back( kid->accept( *visitor ) ); | 
|---|
|  | 592 | } catch ( SemanticErrorException & e ) { | 
|---|
|  | 593 | errors.append( e ); | 
|---|
|  | 594 | } | 
|---|
|  | 595 |  | 
|---|
| [2f52b18] | 596 | if ( ! break_label.empty() ) { | 
|---|
|  | 597 | ret.push_back( labelledNullStmt( ret.back()->location, break_label ) ); | 
|---|
| [66daee4] | 598 | break_label = Label( CodeLocation(), "" ); | 
|---|
| [b8ab91a] | 599 | } | 
|---|
|  | 600 | } | 
|---|
|  | 601 |  | 
|---|
| [2f52b18] | 602 | if ( ! errors.isEmpty() ) { | 
|---|
| [b8ab91a] | 603 | throw errors; | 
|---|
|  | 604 | } | 
|---|
|  | 605 | return ret; | 
|---|
|  | 606 | } | 
|---|
|  | 607 |  | 
|---|
| [66daee4] | 608 | const CompoundStmt * multiLevelExitUpdate( | 
|---|
|  | 609 | const CompoundStmt * stmt, | 
|---|
|  | 610 | const LabelToStmt & labelTable ) { | 
|---|
| [b8ab91a] | 611 | // Must start in the body, so FunctionDecls can be a stopping point. | 
|---|
| [66daee4] | 612 | Pass<MultiLevelExitCore> visitor( labelTable ); | 
|---|
|  | 613 | const CompoundStmt * ret = stmt->accept( visitor ); | 
|---|
| [817bb3c] | 614 | return ret; | 
|---|
| [b8ab91a] | 615 | } | 
|---|
|  | 616 | } // namespace ControlStruct | 
|---|
|  | 617 |  | 
|---|
|  | 618 | // Local Variables: // | 
|---|
|  | 619 | // tab-width: 4 // | 
|---|
|  | 620 | // mode: c++ // | 
|---|
|  | 621 | // compile-command: "make install" // | 
|---|
|  | 622 | // End: // | 
|---|