| [9f5ecf5] | 1 | //
 | 
|---|
 | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // Waitfor.cc --
 | 
|---|
 | 8 | //
 | 
|---|
 | 9 | // Author           : Thierry Delisle
 | 
|---|
 | 10 | // Created On       : Mon Aug 28 11:06:52 2017
 | 
|---|
 | 11 | // Last Modified By :
 | 
|---|
 | 12 | // Last Modified On :
 | 
|---|
| [cca568e] | 13 | // Update Count     : 12
 | 
|---|
| [9f5ecf5] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include "Concurrency/Keywords.h"
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | #include <cassert>                 // for assert
 | 
|---|
 | 19 | #include <string>                  // for string, operator==
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | using namespace std::string_literals;
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | #include "Common/PassVisitor.h"    // for PassVisitor
 | 
|---|
 | 24 | #include "Common/SemanticError.h"  // for SemanticError
 | 
|---|
| [312029a] | 25 | #include "Common/UniqueName.h"     // for UniqueName
 | 
|---|
| [9f5ecf5] | 26 | #include "Common/utility.h"        // for deleteAll, map_range
 | 
|---|
 | 27 | #include "CodeGen/OperatorTable.h" // for isConstructor
 | 
|---|
 | 28 | #include "InitTweak/InitTweak.h"   // for getPointerBase
 | 
|---|
| [1dcd9554] | 29 | #include "ResolvExpr/Resolver.h"   // for findVoidExpression
 | 
|---|
| [07de76b] | 30 | #include "SynTree/LinkageSpec.h"   // for Cforall
 | 
|---|
| [9f5ecf5] | 31 | #include "SynTree/Constant.h"      // for Constant
 | 
|---|
 | 32 | #include "SynTree/Declaration.h"   // for StructDecl, FunctionDecl, ObjectDecl
 | 
|---|
 | 33 | #include "SynTree/Expression.h"    // for VariableExpr, ConstantExpr, Untype...
 | 
|---|
 | 34 | #include "SynTree/Initializer.h"   // for SingleInit, ListInit, Initializer ...
 | 
|---|
 | 35 | #include "SynTree/Label.h"         // for Label
 | 
|---|
 | 36 | #include "SynTree/Statement.h"     // for CompoundStmt, DeclStmt, ExprStmt
 | 
|---|
 | 37 | #include "SynTree/Type.h"          // for StructInstType, Type, PointerType
 | 
|---|
 | 38 | #include "SynTree/Visitor.h"       // for Visitor, acceptAll
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | class Attribute;
 | 
|---|
 | 41 | /*
 | 
|---|
 | 42 | void foo() {
 | 
|---|
 | 43 |         while( true ) {
 | 
|---|
| [cca568e] | 44 |                 when( a < 1 ) waitfor( f : a ) { bar(); }
 | 
|---|
| [9f5ecf5] | 45 |                 or timeout( swagl() );
 | 
|---|
| [cca568e] | 46 |                 or waitfor( g : a ) { baz(); }
 | 
|---|
 | 47 |                 or waitfor( ^?{} : a ) { break; }
 | 
|---|
| [9f5ecf5] | 48 |                 or waitfor( ^?{} ) { break; }
 | 
|---|
 | 49 |         }
 | 
|---|
 | 50 | }
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | void f(int i, float f, A & mutex b, struct foo *  );
 | 
|---|
 | 53 | void f(int );
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |                       |  |
 | 
|---|
 | 57 |                       |  |
 | 
|---|
 | 58 |                             |  |
 | 
|---|
 | 59 |                       |  |
 | 
|---|
 | 60 |                       |  |
 | 
|---|
 | 61 |                     \ |  | /
 | 
|---|
 | 62 |                      \    /
 | 
|---|
 | 63 |                       \  /
 | 
|---|
 | 64 |                        \/
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 | void foo() {
 | 
|---|
 | 68 |         while( true ) {
 | 
|---|
| [42506e1] | 69 |                 {
 | 
|---|
 | 70 |                         acceptable_t acceptables[3];
 | 
|---|
 | 71 |                         if( a < 1 ) {
 | 
|---|
 | 72 |                                 acceptables[0].func = f;
 | 
|---|
 | 73 |                                 acceptables[0].mon = a;
 | 
|---|
 | 74 |                         }
 | 
|---|
 | 75 |                         acceptables[1].func = g;
 | 
|---|
 | 76 |                         acceptables[1].mon = a;
 | 
|---|
| [9f5ecf5] | 77 | 
 | 
|---|
| [42506e1] | 78 |                         acceptables[2].func = f;
 | 
|---|
 | 79 |                         acceptables[2].mon = a;
 | 
|---|
 | 80 |                         acceptables[2].is_dtor = true;
 | 
|---|
| [9f5ecf5] | 81 | 
 | 
|---|
| [42506e1] | 82 |                         int ret = waitfor_internal( acceptables, swagl() );
 | 
|---|
| [9f5ecf5] | 83 | 
 | 
|---|
| [42506e1] | 84 |                         switch( ret ) {
 | 
|---|
 | 85 |                                 case 0:
 | 
|---|
 | 86 |                                 {
 | 
|---|
 | 87 |                                         bar();
 | 
|---|
 | 88 |                                 }
 | 
|---|
 | 89 |                                 case 1:
 | 
|---|
| [9f5ecf5] | 90 |                                 {
 | 
|---|
| [42506e1] | 91 |                                         baz();
 | 
|---|
| [9f5ecf5] | 92 |                                 }
 | 
|---|
| [42506e1] | 93 |                                 case 2:
 | 
|---|
 | 94 |                                         signal(a);
 | 
|---|
 | 95 |                                         {
 | 
|---|
 | 96 |                                                 break;
 | 
|---|
 | 97 |                                         }
 | 
|---|
 | 98 |                         }
 | 
|---|
| [9f5ecf5] | 99 |                 }
 | 
|---|
 | 100 |         }
 | 
|---|
 | 101 | }*/
 | 
|---|
 | 102 | 
 | 
|---|
 | 103 | namespace Concurrency {
 | 
|---|
 | 104 |         //=============================================================================================
 | 
|---|
 | 105 |         // Pass declarations
 | 
|---|
 | 106 |         //=============================================================================================
 | 
|---|
 | 107 | 
 | 
|---|
| [1dcd9554] | 108 |         class GenerateWaitForPass final : public WithIndexer {
 | 
|---|
| [9f5ecf5] | 109 |           public:
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |                 void premutate( FunctionDecl * decl );
 | 
|---|
 | 112 |                 void premutate( StructDecl   * decl );
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 |                 Statement * postmutate( WaitForStmt * stmt );
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 |                 static void generate( std::list< Declaration * > & translationUnit ) {
 | 
|---|
 | 117 |                         PassVisitor< GenerateWaitForPass > impl;
 | 
|---|
 | 118 |                         acceptAll( translationUnit, impl );
 | 
|---|
 | 119 |                 }
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 |                 ObjectDecl * declare( unsigned long count, CompoundStmt * stmt );
 | 
|---|
| [aaa4f93] | 122 |                 ObjectDecl * declareFlag( CompoundStmt * stmt );
 | 
|---|
 | 123 |                 Statement  * makeSetter( ObjectDecl * flag );
 | 
|---|
| [9f5ecf5] | 124 |                 ObjectDecl * declMon( WaitForStmt::Clause & clause, CompoundStmt * stmt );
 | 
|---|
| [aaa4f93] | 125 |                 void         init( ObjectDecl * acceptables, int index, WaitForStmt::Clause & clause, Statement * settter, CompoundStmt * stmt );
 | 
|---|
 | 126 |                 Expression * init_timeout( Expression *& time, Expression *& time_cond, bool has_else, Expression *& else_cond, Statement * settter, CompoundStmt * stmt );
 | 
|---|
| [1dcd9554] | 127 |                 Expression * call(size_t count, ObjectDecl * acceptables, Expression * timeout, CompoundStmt * stmt);
 | 
|---|
 | 128 |                 void         choose( WaitForStmt * waitfor, Expression  * result, CompoundStmt * stmt );
 | 
|---|
| [9f5ecf5] | 129 | 
 | 
|---|
 | 130 |                 static void implement( std::list< Declaration * > & translationUnit ) {
 | 
|---|
 | 131 |                         PassVisitor< GenerateWaitForPass > impl;
 | 
|---|
 | 132 |                         mutateAll( translationUnit, impl );
 | 
|---|
 | 133 |                 }
 | 
|---|
 | 134 | 
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |           private:
 | 
|---|
 | 137 |                 FunctionDecl        * decl_waitfor    = nullptr;
 | 
|---|
| [aaa4f93] | 138 |                 StructDecl          * decl_mask       = nullptr;
 | 
|---|
| [9f5ecf5] | 139 |                 StructDecl          * decl_acceptable = nullptr;
 | 
|---|
 | 140 |                 StructDecl          * decl_monitor    = nullptr;
 | 
|---|
 | 141 | 
 | 
|---|
 | 142 |                 static std::unique_ptr< Type > generic_func;
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 |                 UniqueName namer_acc = "__acceptables_"s;
 | 
|---|
| [aaa4f93] | 145 |                 UniqueName namer_idx = "__index_"s;
 | 
|---|
 | 146 |                 UniqueName namer_flg = "__do_run_"s;
 | 
|---|
 | 147 |                 UniqueName namer_msk = "__mask_"s;
 | 
|---|
 | 148 |                 UniqueName namer_mon = "__monitors_"s;
 | 
|---|
| [9f5ecf5] | 149 |                 UniqueName namer_tim = "__timeout_"s;
 | 
|---|
 | 150 |         };
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 |         //=============================================================================================
 | 
|---|
 | 153 |         // General entry routine
 | 
|---|
 | 154 |         //=============================================================================================
 | 
|---|
 | 155 |         void generateWaitFor( std::list< Declaration * > & translationUnit ) {
 | 
|---|
 | 156 |                 GenerateWaitForPass     ::implement( translationUnit );
 | 
|---|
 | 157 |         }
 | 
|---|
 | 158 | 
 | 
|---|
 | 159 |         //=============================================================================================
 | 
|---|
 | 160 |         // Generic helper routine
 | 
|---|
 | 161 |         //=============================================================================================
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 |         namespace {
 | 
|---|
 | 164 |                 Expression * makeOpIndex( DeclarationWithType * array, unsigned long index ) {
 | 
|---|
| [1dcd9554] | 165 |                         return new UntypedExpr(
 | 
|---|
| [9f5ecf5] | 166 |                                 new NameExpr( "?[?]" ),
 | 
|---|
 | 167 |                                 {
 | 
|---|
 | 168 |                                         new VariableExpr( array ),
 | 
|---|
 | 169 |                                         new ConstantExpr( Constant::from_ulong( index ) )
 | 
|---|
 | 170 |                                 }
 | 
|---|
 | 171 |                         );
 | 
|---|
 | 172 |                 }
 | 
|---|
 | 173 | 
 | 
|---|
 | 174 |                 Expression * makeOpAssign( Expression * lhs, Expression * rhs ) {
 | 
|---|
| [1dcd9554] | 175 |                         return new UntypedExpr(
 | 
|---|
| [9f5ecf5] | 176 |                                         new NameExpr( "?=?" ),
 | 
|---|
 | 177 |                                         { lhs, rhs }
 | 
|---|
 | 178 |                         );
 | 
|---|
 | 179 |                 }
 | 
|---|
 | 180 | 
 | 
|---|
| [1dcd9554] | 181 |                 Expression * makeOpMember( Expression * sue, const std::string & mem ) {
 | 
|---|
 | 182 |                         return new UntypedMemberExpr( new NameExpr( mem ), sue );
 | 
|---|
| [9f5ecf5] | 183 |                 }
 | 
|---|
 | 184 | 
 | 
|---|
| [1dcd9554] | 185 |                 Statement * makeAccStatement( DeclarationWithType * object, unsigned long index, const std::string & member, Expression * value, const SymTab::Indexer & indexer ) {
 | 
|---|
| [08da53d] | 186 |                         Expression * expr = makeOpAssign(
 | 
|---|
| [1dcd9554] | 187 |                                 makeOpMember(
 | 
|---|
 | 188 |                                         makeOpIndex(
 | 
|---|
 | 189 |                                                 object,
 | 
|---|
 | 190 |                                                 index
 | 
|---|
| [9f5ecf5] | 191 |                                         ),
 | 
|---|
| [1dcd9554] | 192 |                                         member
 | 
|---|
 | 193 |                                 ),
 | 
|---|
 | 194 |                                 value
 | 
|---|
| [08da53d] | 195 |                         );
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 |                         ResolvExpr::findVoidExpression( expr, indexer );
 | 
|---|
| [1dcd9554] | 198 | 
 | 
|---|
| [ba3706f] | 199 |                         return new ExprStmt( expr );
 | 
|---|
| [9f5ecf5] | 200 |                 }
 | 
|---|
 | 201 | 
 | 
|---|
 | 202 |                 Expression * safeCond( Expression * expr, bool ifnull = true ) {
 | 
|---|
 | 203 |                         if( expr ) return expr;
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 |                         return new ConstantExpr( Constant::from_bool( ifnull ) );
 | 
|---|
 | 206 |                 }
 | 
|---|
| [310e5b7] | 207 | 
 | 
|---|
 | 208 |                 VariableExpr * extractVariable( Expression * func ) {
 | 
|---|
 | 209 |                         if( VariableExpr * var = dynamic_cast< VariableExpr * >( func ) ) {
 | 
|---|
 | 210 |                                 return var;
 | 
|---|
 | 211 |                         }
 | 
|---|
 | 212 | 
 | 
|---|
 | 213 |                         CastExpr * cast = strict_dynamic_cast< CastExpr * >( func );
 | 
|---|
 | 214 |                         return strict_dynamic_cast< VariableExpr * >( cast->arg );
 | 
|---|
 | 215 |                 }
 | 
|---|
 | 216 | 
 | 
|---|
| [8f98b78] | 217 |                 Expression * detectIsDtor( Expression * func ) {
 | 
|---|
| [310e5b7] | 218 |                         VariableExpr * typed_func = extractVariable( func );
 | 
|---|
 | 219 |                         bool is_dtor = InitTweak::isDestructor( typed_func->var );
 | 
|---|
 | 220 |                         return new ConstantExpr( Constant::from_bool( is_dtor ) );
 | 
|---|
 | 221 |                 }
 | 
|---|
| [9f5ecf5] | 222 |         };
 | 
|---|
 | 223 | 
 | 
|---|
 | 224 | 
 | 
|---|
 | 225 |         //=============================================================================================
 | 
|---|
 | 226 |         // Generate waitfor implementation
 | 
|---|
 | 227 |         //=============================================================================================
 | 
|---|
 | 228 | 
 | 
|---|
 | 229 |         void GenerateWaitForPass::premutate( FunctionDecl * decl) {
 | 
|---|
| [310e5b7] | 230 |                 if( decl->name != "__waitfor_internal" ) return;
 | 
|---|
| [9f5ecf5] | 231 | 
 | 
|---|
 | 232 |                 decl_waitfor = decl;
 | 
|---|
 | 233 |         }
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 |         void GenerateWaitForPass::premutate( StructDecl   * decl ) {
 | 
|---|
 | 236 |                 if( ! decl->body ) return;
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 |                 if( decl->name == "__acceptable_t" ) {
 | 
|---|
 | 239 |                         assert( !decl_acceptable );
 | 
|---|
 | 240 |                         decl_acceptable = decl;
 | 
|---|
 | 241 |                 }
 | 
|---|
| [aaa4f93] | 242 |                 else if( decl->name == "__waitfor_mask_t" ) {
 | 
|---|
 | 243 |                         assert( !decl_mask );
 | 
|---|
 | 244 |                         decl_mask = decl;
 | 
|---|
 | 245 |                 }
 | 
|---|
| [ac2b598] | 246 |                 else if( decl->name == "$monitor" ) {
 | 
|---|
| [9f5ecf5] | 247 |                         assert( !decl_monitor );
 | 
|---|
 | 248 |                         decl_monitor = decl;
 | 
|---|
 | 249 |                 }
 | 
|---|
 | 250 |         }
 | 
|---|
 | 251 | 
 | 
|---|
 | 252 |         Statement * GenerateWaitForPass::postmutate( WaitForStmt * waitfor ) {
 | 
|---|
| [d55d7a6] | 253 |                 if( !decl_monitor || !decl_acceptable || !decl_mask )
 | 
|---|
| [73abe95] | 254 |                         SemanticError( waitfor, "waitfor keyword requires monitors to be in scope, add #include <monitor.hfa>" );
 | 
|---|
| [9f5ecf5] | 255 | 
 | 
|---|
| [ba3706f] | 256 |                 CompoundStmt * stmt = new CompoundStmt();
 | 
|---|
| [9f5ecf5] | 257 | 
 | 
|---|
 | 258 |                 ObjectDecl * acceptables = declare( waitfor->clauses.size(), stmt );
 | 
|---|
| [aaa4f93] | 259 |                 ObjectDecl * flag        = declareFlag( stmt );
 | 
|---|
 | 260 |                 Statement  * setter      = makeSetter( flag );
 | 
|---|
| [9f5ecf5] | 261 | 
 | 
|---|
 | 262 |                 int index = 0;
 | 
|---|
 | 263 |                 for( auto & clause : waitfor->clauses ) {
 | 
|---|
| [aaa4f93] | 264 |                         init( acceptables, index, clause, setter, stmt );
 | 
|---|
| [9f5ecf5] | 265 | 
 | 
|---|
 | 266 |                         index++;
 | 
|---|
 | 267 |                 }
 | 
|---|
 | 268 | 
 | 
|---|
 | 269 |                 Expression * timeout = init_timeout(
 | 
|---|
 | 270 |                         waitfor->timeout.time,
 | 
|---|
 | 271 |                         waitfor->timeout.condition,
 | 
|---|
 | 272 |                         waitfor->orelse .statement,
 | 
|---|
 | 273 |                         waitfor->orelse .condition,
 | 
|---|
| [aaa4f93] | 274 |                         setter,
 | 
|---|
| [9f5ecf5] | 275 |                         stmt
 | 
|---|
 | 276 |                 );
 | 
|---|
 | 277 | 
 | 
|---|
| [ba3706f] | 278 |                 CompoundStmt * compound = new CompoundStmt();
 | 
|---|
| [aaa4f93] | 279 |                 stmt->push_back( new IfStmt(
 | 
|---|
 | 280 |                         safeCond( new VariableExpr( flag ) ),
 | 
|---|
 | 281 |                         compound,
 | 
|---|
 | 282 |                         nullptr
 | 
|---|
 | 283 |                 ));
 | 
|---|
 | 284 | 
 | 
|---|
 | 285 |                 Expression * result = call( waitfor->clauses.size(), acceptables, timeout, compound );
 | 
|---|
| [9f5ecf5] | 286 | 
 | 
|---|
| [aaa4f93] | 287 |                 choose( waitfor, result, compound );
 | 
|---|
| [9f5ecf5] | 288 | 
 | 
|---|
 | 289 |                 return stmt;
 | 
|---|
 | 290 |         }
 | 
|---|
 | 291 | 
 | 
|---|
 | 292 |         ObjectDecl * GenerateWaitForPass::declare( unsigned long count, CompoundStmt * stmt )
 | 
|---|
 | 293 |         {
 | 
|---|
| [1dcd9554] | 294 |                 ObjectDecl * acceptables = ObjectDecl::newObject(
 | 
|---|
| [9f5ecf5] | 295 |                         namer_acc.newName(),
 | 
|---|
 | 296 |                         new ArrayType(
 | 
|---|
 | 297 |                                 noQualifiers,
 | 
|---|
 | 298 |                                 new StructInstType(
 | 
|---|
 | 299 |                                         noQualifiers,
 | 
|---|
 | 300 |                                         decl_acceptable
 | 
|---|
 | 301 |                                 ),
 | 
|---|
 | 302 |                                 new ConstantExpr( Constant::from_ulong( count ) ),
 | 
|---|
 | 303 |                                 false,
 | 
|---|
 | 304 |                                 false
 | 
|---|
 | 305 |                         ),
 | 
|---|
 | 306 |                         nullptr
 | 
|---|
 | 307 |                 );
 | 
|---|
 | 308 | 
 | 
|---|
| [ba3706f] | 309 |                 stmt->push_back( new DeclStmt( acceptables) );
 | 
|---|
| [9f5ecf5] | 310 | 
 | 
|---|
| [08da53d] | 311 |                 Expression * set = new UntypedExpr(
 | 
|---|
| [aaa4f93] | 312 |                         new NameExpr( "__builtin_memset" ),
 | 
|---|
 | 313 |                         {
 | 
|---|
 | 314 |                                 new VariableExpr( acceptables ),
 | 
|---|
 | 315 |                                 new ConstantExpr( Constant::from_int( 0 ) ),
 | 
|---|
 | 316 |                                 new SizeofExpr( new VariableExpr( acceptables ) )
 | 
|---|
 | 317 |                         }
 | 
|---|
 | 318 |                 );
 | 
|---|
 | 319 | 
 | 
|---|
| [08da53d] | 320 |                 ResolvExpr::findVoidExpression( set, indexer );
 | 
|---|
| [aaa4f93] | 321 | 
 | 
|---|
| [ba3706f] | 322 |                 stmt->push_back( new ExprStmt( set ) );
 | 
|---|
| [aaa4f93] | 323 | 
 | 
|---|
| [9f5ecf5] | 324 |                 return acceptables;
 | 
|---|
 | 325 |         }
 | 
|---|
 | 326 | 
 | 
|---|
| [aaa4f93] | 327 |         ObjectDecl * GenerateWaitForPass::declareFlag( CompoundStmt * stmt ) {
 | 
|---|
 | 328 |                 ObjectDecl * flag = ObjectDecl::newObject(
 | 
|---|
 | 329 |                         namer_flg.newName(),
 | 
|---|
 | 330 |                         new BasicType(
 | 
|---|
 | 331 |                                 noQualifiers,
 | 
|---|
| [e15853c] | 332 |                                 BasicType::Bool
 | 
|---|
| [aaa4f93] | 333 |                         ),
 | 
|---|
 | 334 |                         new SingleInit( new ConstantExpr( Constant::from_ulong( 0 ) ) )
 | 
|---|
 | 335 |                 );
 | 
|---|
 | 336 | 
 | 
|---|
| [ba3706f] | 337 |                 stmt->push_back( new DeclStmt( flag) );
 | 
|---|
| [aaa4f93] | 338 | 
 | 
|---|
 | 339 |                 return flag;
 | 
|---|
 | 340 |         }
 | 
|---|
 | 341 | 
 | 
|---|
 | 342 |         Statement * GenerateWaitForPass::makeSetter( ObjectDecl * flag ) {
 | 
|---|
| [08da53d] | 343 |                 Expression * expr = new UntypedExpr(
 | 
|---|
| [aaa4f93] | 344 |                         new NameExpr( "?=?" ),
 | 
|---|
 | 345 |                         {
 | 
|---|
 | 346 |                                 new VariableExpr( flag ),
 | 
|---|
 | 347 |                                 new ConstantExpr( Constant::from_ulong( 1 ) )
 | 
|---|
 | 348 |                         }
 | 
|---|
 | 349 |                 );
 | 
|---|
 | 350 | 
 | 
|---|
| [08da53d] | 351 |                 ResolvExpr::findVoidExpression( expr, indexer );
 | 
|---|
| [aaa4f93] | 352 | 
 | 
|---|
| [ba3706f] | 353 |                 return new ExprStmt( expr );
 | 
|---|
| [aaa4f93] | 354 |         }
 | 
|---|
 | 355 | 
 | 
|---|
| [9f5ecf5] | 356 |         ObjectDecl * GenerateWaitForPass::declMon( WaitForStmt::Clause & clause, CompoundStmt * stmt ) {
 | 
|---|
 | 357 | 
 | 
|---|
| [1dcd9554] | 358 |                 ObjectDecl * mon = ObjectDecl::newObject(
 | 
|---|
| [9f5ecf5] | 359 |                         namer_mon.newName(),
 | 
|---|
 | 360 |                         new ArrayType(
 | 
|---|
 | 361 |                                 noQualifiers,
 | 
|---|
| [8f98b78] | 362 |                                 new PointerType(
 | 
|---|
| [9f5ecf5] | 363 |                                         noQualifiers,
 | 
|---|
| [8f98b78] | 364 |                                         new StructInstType(
 | 
|---|
 | 365 |                                                 noQualifiers,
 | 
|---|
 | 366 |                                                 decl_monitor
 | 
|---|
 | 367 |                                         )
 | 
|---|
| [9f5ecf5] | 368 |                                 ),
 | 
|---|
 | 369 |                                 new ConstantExpr( Constant::from_ulong( clause.target.arguments.size() ) ),
 | 
|---|
 | 370 |                                 false,
 | 
|---|
 | 371 |                                 false
 | 
|---|
 | 372 |                         ),
 | 
|---|
 | 373 |                         new ListInit(
 | 
|---|
 | 374 |                                 map_range < std::list<Initializer*> > ( clause.target.arguments, [this](Expression * expr ){
 | 
|---|
| [08da53d] | 375 |                                         Expression * init = new CastExpr(
 | 
|---|
| [8f98b78] | 376 |                                                 new UntypedExpr(
 | 
|---|
 | 377 |                                                         new NameExpr( "get_monitor" ),
 | 
|---|
 | 378 |                                                         { expr }
 | 
|---|
 | 379 |                                                 ),
 | 
|---|
 | 380 |                                                 new PointerType(
 | 
|---|
 | 381 |                                                         noQualifiers,
 | 
|---|
 | 382 |                                                         new StructInstType(
 | 
|---|
 | 383 |                                                                 noQualifiers,
 | 
|---|
 | 384 |                                                                 decl_monitor
 | 
|---|
 | 385 |                                                         )
 | 
|---|
 | 386 |                                                 )
 | 
|---|
 | 387 |                                         );
 | 
|---|
 | 388 | 
 | 
|---|
| [08da53d] | 389 |                                         ResolvExpr::findSingleExpression( init, indexer );
 | 
|---|
| [8f98b78] | 390 |                                         return new SingleInit( init );
 | 
|---|
| [9f5ecf5] | 391 |                                 })
 | 
|---|
 | 392 |                         )
 | 
|---|
 | 393 |                 );
 | 
|---|
 | 394 | 
 | 
|---|
| [ba3706f] | 395 |                 stmt->push_back( new DeclStmt( mon) );
 | 
|---|
| [9f5ecf5] | 396 | 
 | 
|---|
 | 397 |                 return mon;
 | 
|---|
 | 398 |         }
 | 
|---|
 | 399 | 
 | 
|---|
| [aaa4f93] | 400 |         void GenerateWaitForPass::init( ObjectDecl * acceptables, int index, WaitForStmt::Clause & clause, Statement * setter, CompoundStmt * stmt ) {
 | 
|---|
| [9f5ecf5] | 401 | 
 | 
|---|
 | 402 |                 ObjectDecl * monitors = declMon( clause, stmt );
 | 
|---|
 | 403 | 
 | 
|---|
| [1dcd9554] | 404 |                 Type * fptr_t = new PointerType( noQualifiers, new FunctionType( noQualifiers, true ) );
 | 
|---|
 | 405 | 
 | 
|---|
| [9f5ecf5] | 406 |                 stmt->push_back( new IfStmt(
 | 
|---|
 | 407 |                         safeCond( clause.condition ),
 | 
|---|
| [aaa4f93] | 408 |                         new CompoundStmt({
 | 
|---|
 | 409 |                                 makeAccStatement( acceptables, index, "is_dtor", detectIsDtor( clause.target.function )                                    , indexer ),
 | 
|---|
 | 410 |                                 makeAccStatement( acceptables, index, "func"   , new CastExpr( clause.target.function, fptr_t )                            , indexer ),
 | 
|---|
| [0cf5b79] | 411 |                                 makeAccStatement( acceptables, index, "data"   , new VariableExpr( monitors )                                              , indexer ),
 | 
|---|
| [aaa4f93] | 412 |                                 makeAccStatement( acceptables, index, "size"   , new ConstantExpr( Constant::from_ulong( clause.target.arguments.size() ) ), indexer ),
 | 
|---|
 | 413 |                                 setter->clone()
 | 
|---|
 | 414 |                         }),
 | 
|---|
| [9f5ecf5] | 415 |                         nullptr
 | 
|---|
 | 416 |                 ));
 | 
|---|
 | 417 | 
 | 
|---|
 | 418 |                 clause.target.function = nullptr;
 | 
|---|
 | 419 |                 clause.target.arguments.empty();
 | 
|---|
 | 420 |                 clause.condition = nullptr;
 | 
|---|
 | 421 |         }
 | 
|---|
 | 422 | 
 | 
|---|
 | 423 |         Expression * GenerateWaitForPass::init_timeout(
 | 
|---|
 | 424 |                 Expression *& time,
 | 
|---|
 | 425 |                 Expression *& time_cond,
 | 
|---|
 | 426 |                 bool has_else,
 | 
|---|
 | 427 |                 Expression *& else_cond,
 | 
|---|
| [aaa4f93] | 428 |                 Statement * setter,
 | 
|---|
| [9f5ecf5] | 429 |                 CompoundStmt * stmt
 | 
|---|
 | 430 |         ) {
 | 
|---|
| [1dcd9554] | 431 |                 ObjectDecl * timeout = ObjectDecl::newObject(
 | 
|---|
| [9f5ecf5] | 432 |                         namer_tim.newName(),
 | 
|---|
 | 433 |                         new BasicType(
 | 
|---|
 | 434 |                                 noQualifiers,
 | 
|---|
 | 435 |                                 BasicType::LongLongUnsignedInt
 | 
|---|
 | 436 |                         ),
 | 
|---|
 | 437 |                         new SingleInit(
 | 
|---|
 | 438 |                                 new ConstantExpr( Constant::from_int( -1 ) )
 | 
|---|
 | 439 |                         )
 | 
|---|
 | 440 |                 );
 | 
|---|
 | 441 | 
 | 
|---|
| [ba3706f] | 442 |                 stmt->push_back( new DeclStmt( timeout ) );
 | 
|---|
| [9f5ecf5] | 443 | 
 | 
|---|
 | 444 |                 if( time ) {
 | 
|---|
 | 445 |                         stmt->push_back( new IfStmt(
 | 
|---|
| [1dcd9554] | 446 |                                 safeCond( time_cond ),
 | 
|---|
| [aaa4f93] | 447 |                                 new CompoundStmt({
 | 
|---|
 | 448 |                                         new ExprStmt(
 | 
|---|
 | 449 |                                                 makeOpAssign(
 | 
|---|
 | 450 |                                                         new VariableExpr( timeout ),
 | 
|---|
 | 451 |                                                         time
 | 
|---|
 | 452 |                                                 )
 | 
|---|
 | 453 |                                         ),
 | 
|---|
 | 454 |                                         setter->clone()
 | 
|---|
 | 455 |                                 }),
 | 
|---|
| [9f5ecf5] | 456 |                                 nullptr
 | 
|---|
 | 457 |                         ));
 | 
|---|
 | 458 | 
 | 
|---|
 | 459 |                         time = time_cond = nullptr;
 | 
|---|
 | 460 |                 }
 | 
|---|
 | 461 | 
 | 
|---|
 | 462 |                 if( has_else ) {
 | 
|---|
 | 463 |                         stmt->push_back( new IfStmt(
 | 
|---|
 | 464 |                                 safeCond( else_cond ),
 | 
|---|
| [aaa4f93] | 465 |                                 new CompoundStmt({
 | 
|---|
 | 466 |                                         new ExprStmt(
 | 
|---|
 | 467 |                                                 makeOpAssign(
 | 
|---|
 | 468 |                                                         new VariableExpr( timeout ),
 | 
|---|
 | 469 |                                                         new ConstantExpr( Constant::from_ulong( 0 ) )
 | 
|---|
 | 470 |                                                 )
 | 
|---|
 | 471 |                                         ),
 | 
|---|
 | 472 |                                         setter->clone()
 | 
|---|
 | 473 |                                 }),
 | 
|---|
| [9f5ecf5] | 474 |                                 nullptr
 | 
|---|
 | 475 |                         ));
 | 
|---|
 | 476 | 
 | 
|---|
 | 477 |                         else_cond = nullptr;
 | 
|---|
 | 478 |                 }
 | 
|---|
 | 479 | 
 | 
|---|
| [aaa4f93] | 480 |                 delete setter;
 | 
|---|
 | 481 | 
 | 
|---|
| [9f5ecf5] | 482 |                 return new VariableExpr( timeout );
 | 
|---|
 | 483 |         }
 | 
|---|
| [1dcd9554] | 484 | 
 | 
|---|
 | 485 |         Expression * GenerateWaitForPass::call(
 | 
|---|
 | 486 |                 size_t count,
 | 
|---|
 | 487 |                 ObjectDecl * acceptables,
 | 
|---|
 | 488 |                 Expression * timeout,
 | 
|---|
 | 489 |                 CompoundStmt * stmt
 | 
|---|
 | 490 |         ) {
 | 
|---|
| [aaa4f93] | 491 |                 ObjectDecl * index = ObjectDecl::newObject(
 | 
|---|
 | 492 |                         namer_idx.newName(),
 | 
|---|
| [1dcd9554] | 493 |                         new BasicType(
 | 
|---|
 | 494 |                                 noQualifiers,
 | 
|---|
| [aaa4f93] | 495 |                                 BasicType::ShortSignedInt
 | 
|---|
| [1dcd9554] | 496 |                         ),
 | 
|---|
 | 497 |                         new SingleInit(
 | 
|---|
| [aaa4f93] | 498 |                                 new ConstantExpr( Constant::from_int( -1 ) )
 | 
|---|
| [1dcd9554] | 499 |                         )
 | 
|---|
 | 500 |                 );
 | 
|---|
 | 501 | 
 | 
|---|
| [ba3706f] | 502 |                 stmt->push_back( new DeclStmt( index ) );
 | 
|---|
| [aaa4f93] | 503 | 
 | 
|---|
 | 504 |                 ObjectDecl * mask = ObjectDecl::newObject(
 | 
|---|
 | 505 |                         namer_msk.newName(),
 | 
|---|
 | 506 |                         new StructInstType(
 | 
|---|
 | 507 |                                 noQualifiers,
 | 
|---|
 | 508 |                                 decl_mask
 | 
|---|
 | 509 |                         ),
 | 
|---|
 | 510 |                         new ListInit({
 | 
|---|
 | 511 |                                 new SingleInit( new AddressExpr( new VariableExpr( index ) ) ),
 | 
|---|
| [d2d50d7] | 512 |                                 new ListInit({
 | 
|---|
 | 513 |                                         new SingleInit( new VariableExpr( acceptables ) ),
 | 
|---|
 | 514 |                                         new SingleInit( new ConstantExpr( Constant::from_ulong( count ) ) )
 | 
|---|
 | 515 |                                 })
 | 
|---|
| [aaa4f93] | 516 |                         })
 | 
|---|
 | 517 |                 );
 | 
|---|
 | 518 | 
 | 
|---|
| [ba3706f] | 519 |                 stmt->push_back( new DeclStmt( mask ) );
 | 
|---|
| [aaa4f93] | 520 | 
 | 
|---|
 | 521 |                 stmt->push_back( new ExprStmt(
 | 
|---|
 | 522 |                         new ApplicationExpr(
 | 
|---|
 | 523 |                                 VariableExpr::functionPointer( decl_waitfor ),
 | 
|---|
 | 524 |                                 {
 | 
|---|
 | 525 |                                         new CastExpr(
 | 
|---|
 | 526 |                                                 new VariableExpr( mask ),
 | 
|---|
 | 527 |                                                 new ReferenceType(
 | 
|---|
 | 528 |                                                         noQualifiers,
 | 
|---|
 | 529 |                                                         new StructInstType(
 | 
|---|
 | 530 |                                                                 noQualifiers,
 | 
|---|
 | 531 |                                                                 decl_mask
 | 
|---|
 | 532 |                                                         )
 | 
|---|
 | 533 |                                                 )
 | 
|---|
 | 534 |                                         ),
 | 
|---|
 | 535 |                                         timeout
 | 
|---|
 | 536 |                                 }
 | 
|---|
 | 537 |                         )
 | 
|---|
 | 538 |                 ));
 | 
|---|
| [1dcd9554] | 539 | 
 | 
|---|
| [aaa4f93] | 540 |                 return new VariableExpr( index );
 | 
|---|
| [1dcd9554] | 541 |         }
 | 
|---|
 | 542 | 
 | 
|---|
 | 543 |         void GenerateWaitForPass::choose(
 | 
|---|
 | 544 |                 WaitForStmt * waitfor,
 | 
|---|
 | 545 |                 Expression  * result,
 | 
|---|
 | 546 |                 CompoundStmt * stmt
 | 
|---|
 | 547 |         ) {
 | 
|---|
 | 548 |                 SwitchStmt * swtch = new SwitchStmt(
 | 
|---|
 | 549 |                         result,
 | 
|---|
 | 550 |                         std::list<Statement *>()
 | 
|---|
 | 551 |                 );
 | 
|---|
 | 552 | 
 | 
|---|
 | 553 |                 unsigned long i = 0;
 | 
|---|
 | 554 |                 for( auto & clause : waitfor->clauses ) {
 | 
|---|
 | 555 |                         swtch->statements.push_back(
 | 
|---|
 | 556 |                                 new CaseStmt(
 | 
|---|
 | 557 |                                         new ConstantExpr( Constant::from_ulong( i++ ) ),
 | 
|---|
 | 558 |                                         {
 | 
|---|
| [42506e1] | 559 |                                                 new CompoundStmt({
 | 
|---|
 | 560 |                                                         clause.statement,
 | 
|---|
 | 561 |                                                         new BranchStmt(
 | 
|---|
 | 562 |                                                                 "",
 | 
|---|
 | 563 |                                                                 BranchStmt::Break
 | 
|---|
 | 564 |                                                         )
 | 
|---|
 | 565 |                                                 })
 | 
|---|
| [1dcd9554] | 566 |                                         }
 | 
|---|
 | 567 |                                 )
 | 
|---|
 | 568 |                         );
 | 
|---|
 | 569 |                 }
 | 
|---|
 | 570 | 
 | 
|---|
 | 571 |                 if(waitfor->timeout.statement) {
 | 
|---|
 | 572 |                         swtch->statements.push_back(
 | 
|---|
 | 573 |                                 new CaseStmt(
 | 
|---|
| [b18830e] | 574 |                                         new ConstantExpr( Constant::from_int( -2 ) ),
 | 
|---|
| [1dcd9554] | 575 |                                         {
 | 
|---|
| [42506e1] | 576 |                                                 new CompoundStmt({
 | 
|---|
 | 577 |                                                         waitfor->timeout.statement,
 | 
|---|
 | 578 |                                                         new BranchStmt(
 | 
|---|
 | 579 |                                                                 "",
 | 
|---|
 | 580 |                                                                 BranchStmt::Break
 | 
|---|
 | 581 |                                                         )
 | 
|---|
 | 582 |                                                 })
 | 
|---|
| [1dcd9554] | 583 |                                         }
 | 
|---|
 | 584 |                                 )
 | 
|---|
 | 585 |                         );
 | 
|---|
 | 586 |                 }
 | 
|---|
 | 587 | 
 | 
|---|
 | 588 |                 if(waitfor->orelse.statement) {
 | 
|---|
 | 589 |                         swtch->statements.push_back(
 | 
|---|
 | 590 |                                 new CaseStmt(
 | 
|---|
| [b18830e] | 591 |                                         new ConstantExpr( Constant::from_int( -1 ) ),
 | 
|---|
| [1dcd9554] | 592 |                                         {
 | 
|---|
| [42506e1] | 593 |                                                 new CompoundStmt({
 | 
|---|
 | 594 |                                                         waitfor->orelse.statement,
 | 
|---|
 | 595 |                                                         new BranchStmt(
 | 
|---|
 | 596 |                                                                 "",
 | 
|---|
 | 597 |                                                                 BranchStmt::Break
 | 
|---|
 | 598 |                                                         )
 | 
|---|
 | 599 |                                                 })
 | 
|---|
| [1dcd9554] | 600 |                                         }
 | 
|---|
 | 601 |                                 )
 | 
|---|
 | 602 |                         );
 | 
|---|
 | 603 |                 }
 | 
|---|
 | 604 | 
 | 
|---|
 | 605 |                 stmt->push_back( swtch );
 | 
|---|
 | 606 |         }
 | 
|---|
| [9f5ecf5] | 607 | };
 | 
|---|
 | 608 | 
 | 
|---|
 | 609 | // Local Variables: //
 | 
|---|
 | 610 | // mode: c //
 | 
|---|
 | 611 | // tab-width: 4 //
 | 
|---|
| [08da53d] | 612 | // End: //
 | 
|---|