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 | // |
---|
7 | // TupleExpansionNew.cpp -- |
---|
8 | // |
---|
9 | // Author : Henry Xue |
---|
10 | // Created On : Wed Aug 18 12:54:02 2021 |
---|
11 | // Last Modified By : Henry Xue |
---|
12 | // Last Modified On : Wed Aug 18 12:54:02 2021 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | // Currently not working due to unresolved issues with UniqueExpr |
---|
17 | |
---|
18 | #include "Tuples.h" |
---|
19 | |
---|
20 | namespace Tuples { |
---|
21 | namespace { |
---|
22 | struct UniqueExprExpander final : public ast::WithDeclsToAdd<> { |
---|
23 | const ast::Expr * postvisit( const ast::UniqueExpr * unqExpr ); |
---|
24 | std::map< int, const ast::Expr * > decls; // not vector, because order added may not be increasing order |
---|
25 | }; |
---|
26 | } // namespace |
---|
27 | |
---|
28 | void expandUniqueExpr( ast::TranslationUnit & translationUnit ) { |
---|
29 | ast::Pass< UniqueExprExpander >::run( translationUnit ); |
---|
30 | } |
---|
31 | |
---|
32 | namespace { |
---|
33 | const ast::Expr * UniqueExprExpander::postvisit( const ast::UniqueExpr * unqExpr ) { |
---|
34 | const CodeLocation loc = unqExpr->location; |
---|
35 | const int id = unqExpr->id; |
---|
36 | |
---|
37 | // on first time visiting a unique expr with a particular ID, generate the expression that replaces all UniqueExprs with that ID, |
---|
38 | // and lookup on subsequent hits. This ensures that all unique exprs with the same ID reference the same variable. |
---|
39 | if ( ! decls.count( id ) ) { |
---|
40 | ast::ptr< ast::Expr > assignUnq; |
---|
41 | ast::ptr< ast::VariableExpr > var = unqExpr->var; |
---|
42 | if ( unqExpr->object ) { |
---|
43 | // an object was generated to represent this unique expression -- it should be added to the list of declarations now |
---|
44 | declsToAddBefore.push_back( unqExpr->object.as< ast::Decl >() ); |
---|
45 | // deep copy required due to unresolved issues with UniqueExpr |
---|
46 | assignUnq = ast::UntypedExpr::createAssign( loc, var, ast::deepCopy( unqExpr->expr.get() ) ); |
---|
47 | } else { |
---|
48 | ast::ptr< ast::Expr > expr = unqExpr->expr; |
---|
49 | ast::ptr< ast::CommaExpr > commaExpr = expr.strict_as< ast::CommaExpr >(); |
---|
50 | assignUnq = commaExpr->arg1; |
---|
51 | } |
---|
52 | auto finished = new ast::ObjectDecl( loc, toString( "_unq", id, "_finished_" ), new ast::BasicType( ast::BasicType::Kind::Bool ), |
---|
53 | new ast::SingleInit( loc, ast::ConstantExpr::from_int( loc, 0 ) ), {}, ast::Linkage::Cforall ); |
---|
54 | declsToAddBefore.push_back( finished ); |
---|
55 | // (finished ? _unq_expr_N : (_unq_expr_N = <unqExpr->get_expr()>, finished = 1, _unq_expr_N)) |
---|
56 | // This pattern ensures that each unique expression is evaluated once, regardless of evaluation order of the generated C code. |
---|
57 | auto assignFinished = ast::UntypedExpr::createAssign( loc, new ast::VariableExpr( loc, finished ), |
---|
58 | ast::ConstantExpr::from_int( loc, 1 ) ); |
---|
59 | auto condExpr = new ast::ConditionalExpr( loc, new ast::VariableExpr( loc, finished ), var, |
---|
60 | new ast::CommaExpr( loc, new ast::CommaExpr( loc, assignUnq, assignFinished ), var ) ); |
---|
61 | condExpr->result = var->result; |
---|
62 | condExpr->env = unqExpr->env; |
---|
63 | decls[id] = condExpr; |
---|
64 | } |
---|
65 | //delete unqExpr; |
---|
66 | return decls[id]; |
---|
67 | } |
---|
68 | } // namespace |
---|
69 | } // namespace Tuples |
---|