source: src/Concurrency/Waitfor.cc@ 310e5b7

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 310e5b7 was 310e5b7, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Fix some issues with waitfor... it appears to work!

  • Property mode set to 100644
File size: 13.0 KB
Line 
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 :
13// Update Count : 5
14//
15
16#include "Concurrency/Keywords.h"
17
18#include <cassert> // for assert
19#include <string> // for string, operator==
20
21using namespace std::string_literals;
22
23#include "Common/PassVisitor.h" // for PassVisitor
24#include "Common/SemanticError.h" // for SemanticError
25#include "Common/utility.h" // for deleteAll, map_range
26#include "CodeGen/OperatorTable.h" // for isConstructor
27#include "InitTweak/InitTweak.h" // for getPointerBase
28#include "Parser/LinkageSpec.h" // for Cforall
29#include "ResolvExpr/Resolver.h" // for findVoidExpression
30#include "SynTree/Constant.h" // for Constant
31#include "SynTree/Declaration.h" // for StructDecl, FunctionDecl, ObjectDecl
32#include "SynTree/Expression.h" // for VariableExpr, ConstantExpr, Untype...
33#include "SynTree/Initializer.h" // for SingleInit, ListInit, Initializer ...
34#include "SynTree/Label.h" // for Label
35#include "SynTree/Statement.h" // for CompoundStmt, DeclStmt, ExprStmt
36#include "SynTree/Type.h" // for StructInstType, Type, PointerType
37#include "SynTree/Visitor.h" // for Visitor, acceptAll
38
39class Attribute;
40/*
41void foo() {
42 while( true ) {
43 when( a < 1 ) waitfor( f, a ) { bar(); }
44 or timeout( swagl() );
45 or waitfor( g, a ) { baz(); }
46 or waitfor( ^?{}, a ) { break; }
47 or waitfor( ^?{} ) { break; }
48 }
49}
50
51void f(int i, float f, A & mutex b, struct foo * );
52void f(int );
53
54
55 | |
56 | |
57 | |
58 | |
59 | |
60 \ | | /
61 \ /
62 \ /
63 \/
64
65
66void foo() {
67 while( true ) {
68
69 acceptable_t acceptables[3];
70 if( a < 1 ) {
71 acceptables[0].func = f;
72 acceptables[0].mon = a;
73 }
74 acceptables[1].func = g;
75 acceptables[1].mon = a;
76
77 acceptables[2].func = f;
78 acceptables[2].mon = a;
79 acceptables[2].is_dtor = true;
80
81 int ret = waitfor_internal( acceptables, swagl() );
82
83 switch( ret ) {
84 case 0:
85 {
86 bar();
87 }
88 case 1:
89 {
90 baz();
91 }
92 case 2:
93 signal(a);
94 {
95 break;
96 }
97 }
98 }
99}*/
100
101namespace Concurrency {
102
103 namespace {
104 const std::list<Label> noLabels;
105 const std::list< Attribute * > noAttributes;
106 Type::StorageClasses noStorage;
107 Type::Qualifiers noQualifiers;
108 }
109
110 //=============================================================================================
111 // Pass declarations
112 //=============================================================================================
113
114 class GenerateWaitForPass final : public WithIndexer {
115 public:
116
117 void premutate( FunctionDecl * decl );
118 void premutate( StructDecl * decl );
119
120 Statement * postmutate( WaitForStmt * stmt );
121
122 static void generate( std::list< Declaration * > & translationUnit ) {
123 PassVisitor< GenerateWaitForPass > impl;
124 acceptAll( translationUnit, impl );
125 }
126
127 ObjectDecl * declare( unsigned long count, CompoundStmt * stmt );
128 ObjectDecl * declMon( WaitForStmt::Clause & clause, CompoundStmt * stmt );
129 void init( ObjectDecl * acceptables, int index, WaitForStmt::Clause & clause, CompoundStmt * stmt );
130 Expression * init_timeout( Expression *& time, Expression *& time_cond, bool has_else, Expression *& else_cond, CompoundStmt * stmt );
131 Expression * call(size_t count, ObjectDecl * acceptables, Expression * timeout, CompoundStmt * stmt);
132 void choose( WaitForStmt * waitfor, Expression * result, CompoundStmt * stmt );
133
134 static void implement( std::list< Declaration * > & translationUnit ) {
135 PassVisitor< GenerateWaitForPass > impl;
136 mutateAll( translationUnit, impl );
137 }
138
139
140 private:
141 FunctionDecl * decl_waitfor = nullptr;
142 StructDecl * decl_acceptable = nullptr;
143 StructDecl * decl_monitor = nullptr;
144
145 static std::unique_ptr< Type > generic_func;
146
147 UniqueName namer_mon = "__monitors_"s;
148 UniqueName namer_acc = "__acceptables_"s;
149 UniqueName namer_tim = "__timeout_"s;
150 UniqueName namer_ret = "__return_"s;
151 };
152
153 //=============================================================================================
154 // General entry routine
155 //=============================================================================================
156 void generateWaitFor( std::list< Declaration * > & translationUnit ) {
157 GenerateWaitForPass ::implement( translationUnit );
158 }
159
160 //=============================================================================================
161 // Generic helper routine
162 //=============================================================================================
163
164 namespace {
165 Expression * makeOpIndex( DeclarationWithType * array, unsigned long index ) {
166 return new UntypedExpr(
167 new NameExpr( "?[?]" ),
168 {
169 new VariableExpr( array ),
170 new ConstantExpr( Constant::from_ulong( index ) )
171 }
172 );
173 }
174
175 Expression * makeOpAssign( Expression * lhs, Expression * rhs ) {
176 return new UntypedExpr(
177 new NameExpr( "?=?" ),
178 { lhs, rhs }
179 );
180 }
181
182 Expression * makeOpMember( Expression * sue, const std::string & mem ) {
183 return new UntypedMemberExpr( new NameExpr( mem ), sue );
184 }
185
186 Statement * makeAccStatement( DeclarationWithType * object, unsigned long index, const std::string & member, Expression * value, const SymTab::Indexer & indexer ) {
187 std::unique_ptr< Expression > expr( makeOpAssign(
188 makeOpMember(
189 makeOpIndex(
190 object,
191 index
192 ),
193 member
194 ),
195 value
196 ) );
197
198 return new ExprStmt( noLabels, ResolvExpr::findVoidExpression( expr.get(), indexer ) );
199 }
200
201 Expression * safeCond( Expression * expr, bool ifnull = true ) {
202 if( expr ) return expr;
203
204 return new ConstantExpr( Constant::from_bool( ifnull ) );
205 }
206
207 VariableExpr * extractVariable( Expression * func ) {
208 if( VariableExpr * var = dynamic_cast< VariableExpr * >( func ) ) {
209 return var;
210 }
211
212 CastExpr * cast = strict_dynamic_cast< CastExpr * >( func );
213 return strict_dynamic_cast< VariableExpr * >( cast->arg );
214 }
215
216 Expression * betterIsDtor( Expression * func ) {
217 VariableExpr * typed_func = extractVariable( func );
218 bool is_dtor = InitTweak::isDestructor( typed_func->var );
219 return new ConstantExpr( Constant::from_bool( is_dtor ) );
220 }
221 };
222
223
224 //=============================================================================================
225 // Generate waitfor implementation
226 //=============================================================================================
227
228 void GenerateWaitForPass::premutate( FunctionDecl * decl) {
229 if( decl->name != "__waitfor_internal" ) return;
230
231 decl_waitfor = decl;
232 }
233
234 void GenerateWaitForPass::premutate( StructDecl * decl ) {
235 if( ! decl->body ) return;
236
237 if( decl->name == "__acceptable_t" ) {
238 assert( !decl_acceptable );
239 decl_acceptable = decl;
240 }
241 else if( decl->name == "monitor_desc" ) {
242 assert( !decl_monitor );
243 decl_monitor = decl;
244 }
245 }
246
247 Statement * GenerateWaitForPass::postmutate( WaitForStmt * waitfor ) {
248 if( !decl_monitor || !decl_acceptable ) throw SemanticError( "waitfor keyword requires monitors to be in scope, add #include <monitor>", waitfor );
249
250 CompoundStmt * stmt = new CompoundStmt( noLabels );
251
252 ObjectDecl * acceptables = declare( waitfor->clauses.size(), stmt );
253
254 int index = 0;
255 for( auto & clause : waitfor->clauses ) {
256 init( acceptables, index, clause, stmt );
257
258 index++;
259 }
260
261 Expression * timeout = init_timeout(
262 waitfor->timeout.time,
263 waitfor->timeout.condition,
264 waitfor->orelse .statement,
265 waitfor->orelse .condition,
266 stmt
267 );
268
269 Expression * result = call( waitfor->clauses.size(), acceptables, timeout, stmt );
270
271 choose( waitfor, result, stmt );
272
273 return stmt;
274 }
275
276 ObjectDecl * GenerateWaitForPass::declare( unsigned long count, CompoundStmt * stmt )
277 {
278 ObjectDecl * acceptables = ObjectDecl::newObject(
279 namer_acc.newName(),
280 new ArrayType(
281 noQualifiers,
282 new StructInstType(
283 noQualifiers,
284 decl_acceptable
285 ),
286 new ConstantExpr( Constant::from_ulong( count ) ),
287 false,
288 false
289 ),
290 nullptr
291 );
292
293 stmt->push_back( new DeclStmt( noLabels, acceptables) );
294
295 return acceptables;
296 }
297
298 ObjectDecl * GenerateWaitForPass::declMon( WaitForStmt::Clause & clause, CompoundStmt * stmt ) {
299
300 ObjectDecl * mon = ObjectDecl::newObject(
301 namer_mon.newName(),
302 new ArrayType(
303 noQualifiers,
304 new StructInstType(
305 noQualifiers,
306 decl_monitor
307 ),
308 new ConstantExpr( Constant::from_ulong( clause.target.arguments.size() ) ),
309 false,
310 false
311 ),
312 new ListInit(
313 map_range < std::list<Initializer*> > ( clause.target.arguments, [this](Expression * expr ){
314 return new SingleInit( expr );
315 })
316 )
317 );
318
319 stmt->push_back( new DeclStmt( noLabels, mon) );
320
321 return mon;
322 }
323
324 void GenerateWaitForPass::init( ObjectDecl * acceptables, int index, WaitForStmt::Clause & clause, CompoundStmt * stmt ) {
325
326 ObjectDecl * monitors = declMon( clause, stmt );
327
328 Type * fptr_t = new PointerType( noQualifiers, new FunctionType( noQualifiers, true ) );
329
330 Expression * is_dtor = betterIsDtor( clause.target.function );
331 CompoundStmt * compound = new CompoundStmt( noLabels );
332 compound->push_back( makeAccStatement( acceptables, index, "func" , new CastExpr( clause.target.function, fptr_t ) , indexer ) );
333 compound->push_back( makeAccStatement( acceptables, index, "count" , new ConstantExpr( Constant::from_ulong( clause.target.arguments.size() ) ), indexer ) );
334 compound->push_back( makeAccStatement( acceptables, index, "monitors", new VariableExpr( monitors ) , indexer ) );
335 compound->push_back( makeAccStatement( acceptables, index, "is_dtor" , is_dtor , indexer ) );
336
337 stmt->push_back( new IfStmt(
338 noLabels,
339 safeCond( clause.condition ),
340 compound,
341 nullptr
342 ));
343
344 clause.target.function = nullptr;
345 clause.target.arguments.empty();
346 clause.condition = nullptr;
347 }
348
349 Expression * GenerateWaitForPass::init_timeout(
350 Expression *& time,
351 Expression *& time_cond,
352 bool has_else,
353 Expression *& else_cond,
354 CompoundStmt * stmt
355 ) {
356 ObjectDecl * timeout = ObjectDecl::newObject(
357 namer_tim.newName(),
358 new BasicType(
359 noQualifiers,
360 BasicType::LongLongUnsignedInt
361 ),
362 new SingleInit(
363 new ConstantExpr( Constant::from_int( -1 ) )
364 )
365 );
366
367 stmt->push_back( new DeclStmt( noLabels, timeout ) );
368
369 if( time ) {
370 stmt->push_back( new IfStmt(
371 noLabels,
372 safeCond( time_cond ),
373 new ExprStmt(
374 noLabels,
375 makeOpAssign(
376 new VariableExpr( timeout ),
377 time
378 )
379 ),
380 nullptr
381 ));
382
383 time = time_cond = nullptr;
384 }
385
386 if( has_else ) {
387 stmt->push_back( new IfStmt(
388 noLabels,
389 safeCond( else_cond ),
390 new ExprStmt(
391 noLabels,
392 makeOpAssign(
393 new VariableExpr( timeout ),
394 new ConstantExpr( Constant::from_ulong( 0 ) )
395 )
396 ),
397 nullptr
398 ));
399
400 else_cond = nullptr;
401 }
402
403 return new VariableExpr( timeout );
404 }
405
406 Expression * GenerateWaitForPass::call(
407 size_t count,
408 ObjectDecl * acceptables,
409 Expression * timeout,
410 CompoundStmt * stmt
411 ) {
412 ObjectDecl * decl = ObjectDecl::newObject(
413 namer_ret.newName(),
414 new BasicType(
415 noQualifiers,
416 BasicType::LongLongUnsignedInt
417 ),
418 new SingleInit(
419 new UntypedExpr(
420 VariableExpr::functionPointer( decl_waitfor ),
421 {
422 new ConstantExpr( Constant::from_ulong( count ) ),
423 new VariableExpr( acceptables ),
424 timeout
425 }
426 )
427 )
428 );
429
430 stmt->push_back( new DeclStmt( noLabels, decl ) );
431
432 return new VariableExpr( decl );
433 }
434
435 void GenerateWaitForPass::choose(
436 WaitForStmt * waitfor,
437 Expression * result,
438 CompoundStmt * stmt
439 ) {
440 SwitchStmt * swtch = new SwitchStmt(
441 noLabels,
442 result,
443 std::list<Statement *>()
444 );
445
446 unsigned long i = 0;
447 for( auto & clause : waitfor->clauses ) {
448 swtch->statements.push_back(
449 new CaseStmt(
450 noLabels,
451 new ConstantExpr( Constant::from_ulong( i++ ) ),
452 {
453 clause.statement,
454 new BranchStmt(
455 noLabels,
456 "",
457 BranchStmt::Break
458 )
459 }
460 )
461 );
462 }
463
464 if(waitfor->timeout.statement) {
465 swtch->statements.push_back(
466 new CaseStmt(
467 noLabels,
468 new ConstantExpr( Constant::from_ulong( i++ ) ),
469 {
470 waitfor->timeout.statement,
471 new BranchStmt(
472 noLabels,
473 "",
474 BranchStmt::Break
475 )
476 }
477 )
478 );
479 }
480
481 if(waitfor->orelse.statement) {
482 swtch->statements.push_back(
483 new CaseStmt(
484 noLabels,
485 new ConstantExpr( Constant::from_ulong( i++ ) ),
486 {
487 waitfor->orelse.statement,
488 new BranchStmt(
489 noLabels,
490 "",
491 BranchStmt::Break
492 )
493 }
494 )
495 );
496 }
497
498 stmt->push_back( swtch );
499 }
500};
501
502// Local Variables: //
503// mode: c //
504// tab-width: 4 //
505// End: //
Note: See TracBrowser for help on using the repository browser.