| [b910d15] | 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 | //
|
|---|
| [11df881] | 7 | // Tuples.cc -- A collection of tuple operations.
|
|---|
| [b910d15] | 8 | //
|
|---|
| 9 | // Author : Andrew Beach
|
|---|
| 10 | // Created On : Mon Jun 17 14:41:00 2019
|
|---|
| 11 | // Last Modified By : Andrew Beach
|
|---|
| [9939dc3] | 12 | // Last Modified On : Mon May 16 16:15:00 2022
|
|---|
| 13 | // Update Count : 2
|
|---|
| [b910d15] | 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "Tuples.h"
|
|---|
| 17 |
|
|---|
| 18 | #include "AST/Pass.hpp"
|
|---|
| [e01eb4a] | 19 | #include "AST/Inspect.hpp"
|
|---|
| [b910d15] | 20 | #include "AST/LinkageSpec.hpp"
|
|---|
| [9939dc3] | 21 | #include "Common/PassVisitor.h"
|
|---|
| [b910d15] | 22 | #include "InitTweak/InitTweak.h"
|
|---|
| 23 |
|
|---|
| 24 | namespace Tuples {
|
|---|
| 25 |
|
|---|
| 26 | namespace {
|
|---|
| [9939dc3] | 27 | /// Checks if impurity (read: side-effects) may exist in a piece of code.
|
|---|
| 28 | /// Currently gives a very crude approximation, wherein any function
|
|---|
| 29 | /// call expression means the code may be impure.
|
|---|
| 30 | struct ImpurityDetector_old : public WithShortCircuiting {
|
|---|
| 31 | bool const ignoreUnique;
|
|---|
| 32 | bool maybeImpure;
|
|---|
| 33 |
|
|---|
| 34 | ImpurityDetector_old( bool ignoreUnique ) :
|
|---|
| 35 | ignoreUnique( ignoreUnique ), maybeImpure( false )
|
|---|
| 36 | {}
|
|---|
| 37 |
|
|---|
| 38 | void previsit( const ApplicationExpr * appExpr ) {
|
|---|
| 39 | visit_children = false;
|
|---|
| 40 | if ( const DeclarationWithType * function =
|
|---|
| 41 | InitTweak::getFunction( appExpr ) ) {
|
|---|
| 42 | if ( function->linkage == LinkageSpec::Intrinsic ) {
|
|---|
| 43 | if ( function->name == "*?" || function->name == "?[?]" ) {
|
|---|
| 44 | // intrinsic dereference, subscript are pure,
|
|---|
| 45 | // but need to recursively look for impurity
|
|---|
| 46 | visit_children = true;
|
|---|
| 47 | return;
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | maybeImpure = true;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | void previsit( const UntypedExpr * ) {
|
|---|
| 55 | maybeImpure = true;
|
|---|
| 56 | visit_children = false;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void previsit( const UniqueExpr * ) {
|
|---|
| 60 | if ( ignoreUnique ) {
|
|---|
| 61 | // bottom out at unique expression.
|
|---|
| 62 | // The existence of a unique expression doesn't change the purity of an expression.
|
|---|
| 63 | // That is, even if the wrapped expression is impure, the wrapper protects the rest of the expression.
|
|---|
| 64 | visit_children = false;
|
|---|
| 65 | return;
|
|---|
| 66 | }
|
|---|
| 67 | }
|
|---|
| 68 | };
|
|---|
| 69 |
|
|---|
| 70 | bool detectImpurity( const Expression * expr, bool ignoreUnique ) {
|
|---|
| 71 | PassVisitor<ImpurityDetector_old> detector( ignoreUnique );
|
|---|
| 72 | expr->accept( detector );
|
|---|
| 73 | return detector.pass.maybeImpure;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| [b910d15] | 76 | /// Determines if impurity (read: side-effects) may exist in a piece of code. Currently gives
|
|---|
| 77 | /// a very crude approximation, wherein any function call expression means the code may be
|
|---|
| 78 | /// impure.
|
|---|
| 79 | struct ImpurityDetector : public ast::WithShortCircuiting {
|
|---|
| [9939dc3] | 80 | bool result = false;
|
|---|
| [b910d15] | 81 |
|
|---|
| 82 | void previsit( ast::ApplicationExpr const * appExpr ) {
|
|---|
| [e01eb4a] | 83 | if ( ast::DeclWithType const * function = ast::getFunction( appExpr ) ) {
|
|---|
| [b910d15] | 84 | if ( function->linkage == ast::Linkage::Intrinsic
|
|---|
| 85 | && ( function->name == "*?" || function->name == "?[?]" ) ) {
|
|---|
| 86 | return;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| [9939dc3] | 89 | result = true; visit_children = false;
|
|---|
| [b910d15] | 90 | }
|
|---|
| 91 | void previsit( ast::UntypedExpr const * ) {
|
|---|
| [9939dc3] | 92 | result = true; visit_children = false;
|
|---|
| [b910d15] | 93 | }
|
|---|
| [bc92bee] | 94 | };
|
|---|
| [9939dc3] | 95 |
|
|---|
| [bc92bee] | 96 | struct ImpurityDetectorIgnoreUnique : public ImpurityDetector {
|
|---|
| [ba662b9] | 97 | using ImpurityDetector::previsit;
|
|---|
| [b910d15] | 98 | void previsit( ast::UniqueExpr const * ) {
|
|---|
| [bc92bee] | 99 | visit_children = false;
|
|---|
| [b910d15] | 100 | }
|
|---|
| 101 | };
|
|---|
| 102 | } // namespace
|
|---|
| 103 |
|
|---|
| [bc92bee] | 104 | bool maybeImpure( const ast::Expr * expr ) {
|
|---|
| [9939dc3] | 105 | return ast::Pass<ImpurityDetector>::read( expr );
|
|---|
| [bc92bee] | 106 | }
|
|---|
| 107 |
|
|---|
| [b910d15] | 108 | bool maybeImpureIgnoreUnique( const ast::Expr * expr ) {
|
|---|
| [9939dc3] | 109 | return ast::Pass<ImpurityDetectorIgnoreUnique>::read( expr );
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | bool maybeImpure( const Expression * expr ) {
|
|---|
| 113 | return detectImpurity( expr, false );
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | bool maybeImpureIgnoreUnique( const Expression * expr ) {
|
|---|
| 117 | return detectImpurity( expr, true );
|
|---|
| [b910d15] | 118 | }
|
|---|
| 119 |
|
|---|
| 120 | } // namespace Tuples
|
|---|
| 121 |
|
|---|
| 122 | // Local Variables: //
|
|---|
| 123 | // tab-width: 4 //
|
|---|
| 124 | // mode: c++ //
|
|---|
| 125 | // compile-command: "make install" //
|
|---|
| 126 | // End: //
|
|---|