source: src/Concurrency/Waitfor.cc@ 01ba701

ADT ast-experimental pthread-emulation
Last change on this file since 01ba701 was c8f5f7d, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Translated Gen Waitfor, the waitfor statement works in the new ast.

  • Property mode set to 100644
File size: 15.1 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 : 12
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/UniqueName.h" // for UniqueName
26#include "Common/utility.h" // for deleteAll, map_range
27#include "CodeGen/OperatorTable.h" // for isConstructor
28#include "InitTweak/InitTweak.h" // for getPointerBase
29#include "ResolvExpr/Resolver.h" // for findVoidExpression
30#include "SynTree/LinkageSpec.h" // for Cforall
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
40class Attribute;
41/*
42void foo() {
43 while( true ) {
44 when( a < 1 ) waitfor( f : a ) { bar(); }
45 or timeout( swagl() );
46 or waitfor( g : a ) { baz(); }
47 or waitfor( ^?{} : a ) { break; }
48 or waitfor( ^?{} ) { break; }
49 }
50}
51
52void f(int i, float f, A & mutex b, struct foo * );
53void f(int );
54
55
56 | |
57 | |
58 | |
59 | |
60 | |
61 \ | | /
62 \ /
63 \ /
64 \/
65
66
67void foo() {
68 while( true ) {
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;
77
78 acceptables[2].func = f;
79 acceptables[2].mon = a;
80 acceptables[2].is_dtor = true;
81
82 int ret = waitfor_internal( acceptables, swagl() );
83
84 switch( ret ) {
85 case 0:
86 {
87 bar();
88 }
89 case 1:
90 {
91 baz();
92 }
93 case 2:
94 signal(a);
95 {
96 break;
97 }
98 }
99 }
100 }
101}*/
102
103namespace Concurrency {
104 //=============================================================================================
105 // Pass declarations
106 //=============================================================================================
107
108 class GenerateWaitForPass final : public WithIndexer {
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 );
122 ObjectDecl * declareFlag( CompoundStmt * stmt );
123 Statement * makeSetter( ObjectDecl * flag );
124 ObjectDecl * declMon( WaitForStmt::Clause & clause, CompoundStmt * stmt );
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 );
127 Expression * call(size_t count, ObjectDecl * acceptables, Expression * timeout, CompoundStmt * stmt);
128 void choose( WaitForStmt * waitfor, Expression * result, CompoundStmt * stmt );
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;
138 StructDecl * decl_mask = nullptr;
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;
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;
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 ) {
165 return new UntypedExpr(
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 ) {
175 return new UntypedExpr(
176 new NameExpr( "?=?" ),
177 { lhs, rhs }
178 );
179 }
180
181 Expression * makeOpMember( Expression * sue, const std::string & mem ) {
182 return new UntypedMemberExpr( new NameExpr( mem ), sue );
183 }
184
185 Statement * makeAccStatement( DeclarationWithType * object, unsigned long index, const std::string & member, Expression * value, const SymTab::Indexer & indexer ) {
186 Expression * expr = makeOpAssign(
187 makeOpMember(
188 makeOpIndex(
189 object,
190 index
191 ),
192 member
193 ),
194 value
195 );
196
197 ResolvExpr::findVoidExpression( expr, indexer );
198
199 return new ExprStmt( expr );
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 }
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
217 Expression * detectIsDtor( Expression * func ) {
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 }
222 };
223
224
225 //=============================================================================================
226 // Generate waitfor implementation
227 //=============================================================================================
228
229 void GenerateWaitForPass::premutate( FunctionDecl * decl) {
230 if( decl->name != "__waitfor_internal" ) return;
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 }
242 else if( decl->name == "__waitfor_mask_t" ) {
243 assert( !decl_mask );
244 decl_mask = decl;
245 }
246 else if( decl->name == "monitor$" ) {
247 assert( !decl_monitor );
248 decl_monitor = decl;
249 }
250 }
251
252 Statement * GenerateWaitForPass::postmutate( WaitForStmt * waitfor ) {
253 if( !decl_monitor || !decl_acceptable || !decl_mask )
254 SemanticError( waitfor, "waitfor keyword requires monitors to be in scope, add #include <monitor.hfa>" );
255
256 CompoundStmt * stmt = new CompoundStmt();
257
258 ObjectDecl * acceptables = declare( waitfor->clauses.size(), stmt );
259 ObjectDecl * flag = declareFlag( stmt );
260 Statement * setter = makeSetter( flag );
261
262 int index = 0;
263 for( auto & clause : waitfor->clauses ) {
264 init( acceptables, index, clause, setter, stmt );
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,
274 setter,
275 stmt
276 );
277
278 CompoundStmt * compound = new CompoundStmt();
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 );
286
287 choose( waitfor, result, compound );
288
289 return stmt;
290 }
291
292 ObjectDecl * GenerateWaitForPass::declare( unsigned long count, CompoundStmt * stmt )
293 {
294 ObjectDecl * acceptables = ObjectDecl::newObject(
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
309 stmt->push_back( new DeclStmt( acceptables) );
310
311 Expression * set = new UntypedExpr(
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
320 ResolvExpr::findVoidExpression( set, indexer );
321
322 stmt->push_back( new ExprStmt( set ) );
323
324 return acceptables;
325 }
326
327 ObjectDecl * GenerateWaitForPass::declareFlag( CompoundStmt * stmt ) {
328 ObjectDecl * flag = ObjectDecl::newObject(
329 namer_flg.newName(),
330 new BasicType(
331 noQualifiers,
332 BasicType::Bool
333 ),
334 new SingleInit( new ConstantExpr( Constant::from_ulong( 0 ) ) )
335 );
336
337 stmt->push_back( new DeclStmt( flag) );
338
339 return flag;
340 }
341
342 Statement * GenerateWaitForPass::makeSetter( ObjectDecl * flag ) {
343 Expression * expr = new UntypedExpr(
344 new NameExpr( "?=?" ),
345 {
346 new VariableExpr( flag ),
347 new ConstantExpr( Constant::from_ulong( 1 ) )
348 }
349 );
350
351 ResolvExpr::findVoidExpression( expr, indexer );
352
353 return new ExprStmt( expr );
354 }
355
356 ObjectDecl * GenerateWaitForPass::declMon( WaitForStmt::Clause & clause, CompoundStmt * stmt ) {
357
358 ObjectDecl * mon = ObjectDecl::newObject(
359 namer_mon.newName(),
360 new ArrayType(
361 noQualifiers,
362 new PointerType(
363 noQualifiers,
364 new StructInstType(
365 noQualifiers,
366 decl_monitor
367 )
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, [](Expression * expr ){
375 return new SingleInit( expr );
376 })
377 )
378 );
379
380 stmt->push_back( new DeclStmt( mon) );
381
382 return mon;
383 }
384
385 void GenerateWaitForPass::init( ObjectDecl * acceptables, int index, WaitForStmt::Clause & clause, Statement * setter, CompoundStmt * stmt ) {
386
387 ObjectDecl * monitors = declMon( clause, stmt );
388
389 Type * fptr_t = new PointerType( noQualifiers, new FunctionType( noQualifiers, true ) );
390
391 stmt->push_back( new IfStmt(
392 safeCond( clause.condition ),
393 new CompoundStmt({
394 makeAccStatement( acceptables, index, "is_dtor", detectIsDtor( clause.target.function ) , indexer ),
395 makeAccStatement( acceptables, index, "func" , new CastExpr( clause.target.function, fptr_t, false ) , indexer ),
396 makeAccStatement( acceptables, index, "data" , new VariableExpr( monitors ) , indexer ),
397 makeAccStatement( acceptables, index, "size" , new ConstantExpr( Constant::from_ulong( clause.target.arguments.size() ) ), indexer ),
398 setter->clone()
399 }),
400 nullptr
401 ));
402
403 clause.target.function = nullptr;
404 clause.target.arguments.empty();
405 clause.condition = nullptr;
406 }
407
408 Expression * GenerateWaitForPass::init_timeout(
409 Expression *& time,
410 Expression *& time_cond,
411 bool has_else,
412 Expression *& else_cond,
413 Statement * setter,
414 CompoundStmt * stmt
415 ) {
416 ObjectDecl * timeout = ObjectDecl::newObject(
417 namer_tim.newName(),
418 new BasicType(
419 noQualifiers,
420 BasicType::LongLongUnsignedInt
421 ),
422 new SingleInit(
423 new ConstantExpr( Constant::from_int( -1 ) )
424 )
425 );
426
427 stmt->push_back( new DeclStmt( timeout ) );
428
429 if( time ) {
430 stmt->push_back( new IfStmt(
431 safeCond( time_cond ),
432 new CompoundStmt({
433 new ExprStmt(
434 makeOpAssign(
435 new VariableExpr( timeout ),
436 time
437 )
438 ),
439 setter->clone()
440 }),
441 nullptr
442 ));
443
444 time = time_cond = nullptr;
445 }
446
447 if( has_else ) {
448 stmt->push_back( new IfStmt(
449 safeCond( else_cond ),
450 new CompoundStmt({
451 new ExprStmt(
452 makeOpAssign(
453 new VariableExpr( timeout ),
454 new ConstantExpr( Constant::from_ulong( 0 ) )
455 )
456 ),
457 setter->clone()
458 }),
459 nullptr
460 ));
461
462 else_cond = nullptr;
463 }
464
465 delete setter;
466
467 return new VariableExpr( timeout );
468 }
469
470 Expression * GenerateWaitForPass::call(
471 size_t count,
472 ObjectDecl * acceptables,
473 Expression * timeout,
474 CompoundStmt * stmt
475 ) {
476 ObjectDecl * index = ObjectDecl::newObject(
477 namer_idx.newName(),
478 new BasicType(
479 noQualifiers,
480 BasicType::ShortSignedInt
481 ),
482 new SingleInit(
483 new ConstantExpr( Constant::from_int( -1 ) )
484 )
485 );
486
487 stmt->push_back( new DeclStmt( index ) );
488
489 ObjectDecl * mask = ObjectDecl::newObject(
490 namer_msk.newName(),
491 new StructInstType(
492 noQualifiers,
493 decl_mask
494 ),
495 new ListInit({
496 new SingleInit( new AddressExpr( new VariableExpr( index ) ) ),
497 new ListInit({
498 new SingleInit( new VariableExpr( acceptables ) ),
499 new SingleInit( new ConstantExpr( Constant::from_ulong( count ) ) )
500 })
501 })
502 );
503
504 stmt->push_back( new DeclStmt( mask ) );
505
506 stmt->push_back( new ExprStmt(
507 new ApplicationExpr(
508 VariableExpr::functionPointer( decl_waitfor ),
509 {
510 new CastExpr(
511 new VariableExpr( mask ),
512 new ReferenceType(
513 noQualifiers,
514 new StructInstType(
515 noQualifiers,
516 decl_mask
517 )
518 ),
519 false
520 ),
521 timeout
522 }
523 )
524 ));
525
526 return new VariableExpr( index );
527 }
528
529 void GenerateWaitForPass::choose(
530 WaitForStmt * waitfor,
531 Expression * result,
532 CompoundStmt * stmt
533 ) {
534 SwitchStmt * swtch = new SwitchStmt(
535 result,
536 std::list<Statement *>()
537 );
538
539 unsigned long i = 0;
540 for( auto & clause : waitfor->clauses ) {
541 swtch->statements.push_back(
542 new CaseStmt(
543 new ConstantExpr( Constant::from_ulong( i++ ) ),
544 {
545 new CompoundStmt({
546 clause.statement,
547 new BranchStmt(
548 "",
549 BranchStmt::Break
550 )
551 })
552 }
553 )
554 );
555 }
556
557 if(waitfor->timeout.statement) {
558 swtch->statements.push_back(
559 new CaseStmt(
560 new ConstantExpr( Constant::from_int( -2 ) ),
561 {
562 new CompoundStmt({
563 waitfor->timeout.statement,
564 new BranchStmt(
565 "",
566 BranchStmt::Break
567 )
568 })
569 }
570 )
571 );
572 }
573
574 if(waitfor->orelse.statement) {
575 swtch->statements.push_back(
576 new CaseStmt(
577 new ConstantExpr( Constant::from_int( -1 ) ),
578 {
579 new CompoundStmt({
580 waitfor->orelse.statement,
581 new BranchStmt(
582 "",
583 BranchStmt::Break
584 )
585 })
586 }
587 )
588 );
589 }
590
591 stmt->push_back( swtch );
592 }
593};
594
595// Local Variables: //
596// mode: c //
597// tab-width: 4 //
598// End: //
Note: See TracBrowser for help on using the repository browser.