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