source: src/Concurrency/Keywords.cpp@ 11f92fac

Last change on this file since 11f92fac was 35eef3b, checked in by Peter A. Buhr <pabuhr@…>, 8 months ago

updates to prepare for moving location of inline data for new kinds of types

  • Property mode set to 100644
File size: 49.4 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// Keywords.cpp -- Implement concurrency constructs from their keywords.
8//
9// Author : Andrew Beach
10// Created On : Tue Nov 16 9:53:00 2021
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Jan 26 15:16:16 2025
13// Update Count : 15
14//
15
16#include "Concurrency/Keywords.hpp"
17
18#include <iostream>
19
20#include "AST/Copy.hpp"
21#include "AST/Decl.hpp"
22#include "AST/Expr.hpp"
23#include "AST/Inspect.hpp"
24#include "AST/Pass.hpp"
25#include "AST/Stmt.hpp"
26#include "AST/DeclReplacer.hpp"
27#include "AST/TranslationUnit.hpp"
28#include "CodeGen/OperatorTable.hpp"
29#include "Common/Examine.hpp"
30#include "Common/Utility.hpp"
31#include "Common/UniqueName.hpp"
32#include "ControlStruct/LabelGenerator.hpp"
33#include "InitTweak/InitTweak.hpp"
34#include "Virtual/Tables.hpp"
35
36namespace Concurrency {
37
38namespace {
39
40// --------------------------------------------------------------------------
41// Loose Helper Functions:
42
43/// Detect threads constructed with the keyword thread.
44bool isThread( const ast::DeclWithType * decl ) {
45 auto baseType = decl->get_type()->stripDeclarator();
46 auto instType = dynamic_cast<const ast::StructInstType *>( baseType );
47 if ( nullptr == instType ) { return false; }
48 return instType->base->is_thread();
49}
50
51/// Get the virtual type id if given a type name.
52std::string typeIdType( std::string const & exception_name ) {
53 return exception_name.empty() ? std::string()
54 : Virtual::typeIdType( exception_name );
55}
56
57/// Get the vtable type name if given a type name.
58std::string vtableTypeName( std::string const & exception_name ) {
59 return exception_name.empty() ? std::string()
60 : Virtual::vtableTypeName( exception_name );
61}
62
63static ast::Type * mutate_under_references( ast::ptr<ast::Type>& type ) {
64 ast::Type * mutType = type.get_and_mutate();
65 for ( ast::ReferenceType * mutRef
66 ; (mutRef = dynamic_cast<ast::ReferenceType *>( mutType ))
67 ; mutType = mutRef->base.get_and_mutate() );
68 return mutType;
69}
70
71// Describe that it adds the generic parameters and the uses of the generic parameters on the
72// function and first "this" argument.
73ast::FunctionDecl * fixupGenerics(
74 const ast::FunctionDecl * func, const ast::StructDecl * decl ) {
75 const CodeLocation & location = decl->location;
76 // We have to update both the declaration
77 auto mutFunc = ast::mutate( func );
78 auto mutType = mutFunc->type.get_and_mutate();
79
80 if ( decl->params.empty() ) {
81 return mutFunc;
82 }
83
84 assert( 0 != mutFunc->params.size() );
85 assert( 0 != mutType->params.size() );
86
87 // Add the "forall" clause information.
88 for ( const ast::ptr<ast::TypeDecl> & typeParam : decl->params ) {
89 auto typeDecl = ast::deepCopy( typeParam );
90 mutFunc->type_params.push_back( typeDecl );
91 mutType->forall.push_back( new ast::TypeInstType( typeDecl ) );
92 for ( auto & assertion : typeDecl->assertions ) {
93 mutFunc->assertions.push_back( assertion );
94 mutType->assertions.emplace_back(
95 new ast::VariableExpr( location, assertion ) );
96 }
97 typeDecl->assertions.clear();
98 }
99
100 // Even chain_mutate is not powerful enough for this:
101 ast::ptr<ast::Type>& paramType = strict_dynamic_cast<ast::ObjectDecl *>(
102 mutFunc->params[0].get_and_mutate() )->type;
103 auto paramTypeInst = strict_dynamic_cast<ast::StructInstType *>(
104 mutate_under_references( paramType ) );
105 auto typeParamInst = strict_dynamic_cast<ast::StructInstType *>(
106 mutate_under_references( mutType->params[0] ) );
107
108 for ( const ast::ptr<ast::TypeDecl> & typeDecl : mutFunc->type_params ) {
109 paramTypeInst->params.push_back(
110 new ast::TypeExpr( location, new ast::TypeInstType( typeDecl ) ) );
111 typeParamInst->params.push_back(
112 new ast::TypeExpr( location, new ast::TypeInstType( typeDecl ) ) );
113 }
114
115 return mutFunc;
116}
117
118// --------------------------------------------------------------------------
119
120// This type describes the general information used to transform a generator, coroutine, monitor, or
121// thread aggregate kind. A pass is made over the AST in ConcurrentSueKeyword::postvisit looking
122// for each of these kinds. When found, this data structure is filled in with the information from
123// the specific structures below, and handleStruct is called to perform the common changes that
124// augment the aggregate kind with fields and generate appropriate companion routines. The location
125// of any extra fields is specified in addField.
126
127struct ConcurrentSueKeyword : public ast::WithDeclsToAdd {
128 ConcurrentSueKeyword(
129 std::string&& type_name, std::string&& field_name,
130 std::string&& getter_name, std::string&& context_error,
131 std::string&& exception_name,
132 bool needs_main, ast::AggregateDecl::Aggregate cast_target
133 ) :
134 type_name( type_name ), field_name( field_name ),
135 getter_name( getter_name ), context_error( context_error ),
136 exception_name( exception_name ),
137 typeid_name( typeIdType( exception_name ) ),
138 vtable_name( vtableTypeName( exception_name ) ),
139 needs_main( needs_main ), cast_target( cast_target )
140 {}
141
142 virtual ~ConcurrentSueKeyword() {}
143
144 const ast::Decl * postvisit( const ast::StructDecl * decl );
145 const ast::DeclWithType * postvisit( const ast::FunctionDecl * decl );
146 const ast::Expr * postvisit( const ast::KeywordCastExpr * expr );
147
148 struct StructAndField {
149 const ast::StructDecl * decl;
150 const ast::ObjectDecl * field;
151 };
152
153 const ast::StructDecl * handleStruct( const ast::StructDecl * );
154 void handleMain( const ast::FunctionDecl *, const ast::StructInstType * );
155 void addTypeId( const ast::StructDecl * );
156 void addVtableForward( const ast::StructDecl * );
157 const ast::FunctionDecl * forwardDeclare( const ast::StructDecl * );
158 StructAndField addField( const ast::StructDecl * );
159 void addGetRoutines( const ast::ObjectDecl *, const ast::FunctionDecl * );
160 void addLockUnlockRoutines( const ast::StructDecl * );
161
162private:
163 const std::string type_name;
164 const std::string field_name;
165 const std::string getter_name;
166 const std::string context_error;
167 const std::string exception_name;
168 const std::string typeid_name;
169 const std::string vtable_name;
170 const bool needs_main;
171 const ast::AggregateDecl::Aggregate cast_target;
172
173 const ast::StructDecl * type_decl = nullptr;
174 const ast::FunctionDecl * dtor_decl = nullptr;
175 const ast::StructDecl * except_decl = nullptr;
176 const ast::StructDecl * typeid_decl = nullptr;
177 const ast::StructDecl * vtable_decl = nullptr;
178
179};
180
181// Handles generator type declarations:
182//
183// generator MyGenerator { struct MyGenerator {
184// => int __generator_state;
185// int data; int data;
186// a_struct_t more_data; a_struct_t more_data;
187// }; };
188//
189struct GeneratorKeyword final : public ConcurrentSueKeyword {
190 GeneratorKeyword() : ConcurrentSueKeyword(
191 "generator$",
192 "__generator_state",
193 "get_generator",
194 "Unable to find builtin type generator$\n",
195 "",
196 true,
197 ast::AggregateDecl::Generator )
198 {}
199
200 virtual ~GeneratorKeyword() {}
201};
202
203// Handles coroutine type declarations:
204//
205// coroutine MyCoroutine { struct MyCoroutine {
206// => coroutine$ __cor_d;
207// int data; int data;
208// a_struct_t more_data; a_struct_t more_data;
209// }; };
210// static inline coroutine$ * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; }
211//
212struct CoroutineKeyword final : public ConcurrentSueKeyword {
213 CoroutineKeyword() : ConcurrentSueKeyword(
214 "coroutine$",
215 "__cor",
216 "get_coroutine",
217 "coroutine keyword requires coroutines to be in scope, add #include <coroutine.hfa>\n",
218 "CoroutineCancelled",
219 true,
220 ast::AggregateDecl::Coroutine )
221 {}
222
223 virtual ~CoroutineKeyword() {}
224};
225
226// Handles monitor type declarations:
227//
228// monitor MyMonitor { struct MyMonitor {
229// => monitor$ __mon_d;
230// int data; int data;
231// a_struct_t more_data; a_struct_t more_data;
232// }; };
233// static inline monitor$ * get_coroutine( MyMonitor * this ) {
234// return &this->__cor_d;
235// }
236// void lock(MyMonitor & this) {
237// lock(get_monitor(this));
238// }
239// void unlock(MyMonitor & this) {
240// unlock(get_monitor(this));
241// }
242//
243struct MonitorKeyword final : public ConcurrentSueKeyword {
244 MonitorKeyword() : ConcurrentSueKeyword(
245 "monitor$",
246 "__mon",
247 "get_monitor",
248 "monitor keyword requires monitors to be in scope, add #include <monitor.hfa>\n",
249 "",
250 false,
251 ast::AggregateDecl::Monitor )
252 {}
253
254 virtual ~MonitorKeyword() {}
255};
256
257// Handles thread type declarations:
258//
259// thread Mythread { struct MyThread {
260// => thread$ __thrd_d;
261// int data; int data;
262// a_struct_t more_data; a_struct_t more_data;
263// }; };
264// static inline thread$ * get_thread( MyThread * this ) { return &this->__thrd_d; }
265//
266struct ThreadKeyword final : public ConcurrentSueKeyword {
267 ThreadKeyword() : ConcurrentSueKeyword(
268 "thread$",
269 "__thrd",
270 "get_thread",
271 "thread keyword requires threads to be in scope, add #include <thread.hfa>\n",
272 "ThreadCancelled",
273 true,
274 ast::AggregateDecl::Thread )
275 {}
276
277 virtual ~ThreadKeyword() {}
278};
279
280const ast::Decl * ConcurrentSueKeyword::postvisit(
281 const ast::StructDecl * decl ) {
282 if ( !decl->body ) {
283 return decl;
284 } else if ( cast_target == decl->kind ) {
285 return handleStruct( decl );
286 } else if ( type_name == decl->name ) {
287 assert( !type_decl );
288 type_decl = decl;
289 } else if ( exception_name == decl->name ) {
290 assert( !except_decl );
291 except_decl = decl;
292 } else if ( typeid_name == decl->name ) {
293 assert( !typeid_decl );
294 typeid_decl = decl;
295 } else if ( vtable_name == decl->name ) {
296 assert( !vtable_decl );
297 vtable_decl = decl;
298 }
299 return decl;
300}
301
302// Try to get the full definition, but raise an error on conflicts.
303const ast::FunctionDecl * getDefinition(
304 const ast::FunctionDecl * old_decl,
305 const ast::FunctionDecl * new_decl ) {
306 if ( !new_decl->stmts ) {
307 return old_decl;
308 } else if ( !old_decl->stmts ) {
309 return new_decl;
310 } else {
311 assert( !old_decl->stmts || !new_decl->stmts );
312 return nullptr;
313 }
314}
315
316const ast::DeclWithType * ConcurrentSueKeyword::postvisit(
317 const ast::FunctionDecl * decl ) {
318 if ( type_decl && isDestructorFor( decl, type_decl ) ) {
319 // Check for forward declarations, try to get the full definition.
320 dtor_decl = (dtor_decl) ? getDefinition( dtor_decl, decl ) : decl;
321 } else if ( !vtable_name.empty() && decl->has_body() ) {
322 if (const ast::DeclWithType * param = isMainFor( decl, cast_target )) {
323 if ( !vtable_decl ) {
324 SemanticError( decl, context_error );
325 }
326 // Should be safe because of isMainFor.
327 const ast::StructInstType * struct_type =
328 static_cast<const ast::StructInstType *>(
329 static_cast<const ast::ReferenceType *>(
330 param->get_type() )->base.get() );
331
332 handleMain( decl, struct_type );
333 }
334 }
335 return decl;
336}
337
338const ast::Expr * ConcurrentSueKeyword::postvisit(
339 const ast::KeywordCastExpr * expr ) {
340 if ( cast_target == expr->target ) {
341 // Convert `(thread &)ex` to `(thread$ &)*get_thread(ex)`, etc.
342 if ( !type_decl || !dtor_decl ) {
343 SemanticError( expr, context_error );
344 }
345 assert( nullptr == expr->result );
346 auto cast = ast::mutate( expr );
347 cast->result = new ast::ReferenceType( new ast::StructInstType( type_decl ) );
348 cast->concrete_target.field = field_name;
349 cast->concrete_target.getter = getter_name;
350 return cast;
351 }
352 return expr;
353}
354
355const ast::StructDecl * ConcurrentSueKeyword::handleStruct(
356 const ast::StructDecl * decl ) {
357 assert( decl->body );
358
359 if ( !type_decl || !dtor_decl ) {
360 SemanticError( decl, context_error );
361 }
362
363 if ( !exception_name.empty() ) {
364 if ( !typeid_decl || !vtable_decl ) {
365 SemanticError( decl, context_error );
366 }
367 addTypeId( decl );
368 addVtableForward( decl );
369 }
370
371 const ast::FunctionDecl * func = forwardDeclare( decl );
372 StructAndField addFieldRet = addField( decl );
373 decl = addFieldRet.decl;
374 const ast::ObjectDecl * field = addFieldRet.field;
375
376 addGetRoutines( field, func );
377 // Add routines to monitors for use by mutex stmt.
378 if ( ast::AggregateDecl::Monitor == cast_target ) {
379 addLockUnlockRoutines( decl );
380 }
381
382 return decl;
383}
384
385void ConcurrentSueKeyword::handleMain(
386 const ast::FunctionDecl * decl, const ast::StructInstType * type ) {
387 assert( vtable_decl );
388 assert( except_decl );
389
390 const CodeLocation & location = decl->location;
391
392 std::vector<ast::ptr<ast::Expr>> poly_args = {
393 new ast::TypeExpr( location, type ),
394 };
395 ast::ObjectDecl * vtable_object = Virtual::makeVtableInstance(
396 location,
397 "_default_vtable_object_declaration",
398 new ast::StructInstType( vtable_decl, copy( poly_args ) ),
399 type,
400 nullptr
401 );
402 declsToAddAfter.push_back( vtable_object );
403 declsToAddAfter.push_back(
404 new ast::ObjectDecl(
405 location,
406 Virtual::concurrentDefaultVTableName(),
407 new ast::ReferenceType( vtable_object->type, ast::CV::Const ),
408 new ast::SingleInit( location,
409 new ast::VariableExpr( location, vtable_object ) )
410 )
411 );
412 declsToAddAfter.push_back( Virtual::makeGetExceptionFunction(
413 location,
414 vtable_object,
415 new ast::StructInstType( except_decl, copy( poly_args ) )
416 ) );
417}
418
419void ConcurrentSueKeyword::addTypeId( const ast::StructDecl * decl ) {
420 assert( typeid_decl );
421 const CodeLocation & location = decl->location;
422
423 ast::StructInstType * typeid_type =
424 new ast::StructInstType( typeid_decl, ast::CV::Const );
425 typeid_type->params.push_back(
426 new ast::TypeExpr( location, new ast::StructInstType( decl ) ) );
427 declsToAddBefore.push_back(
428 Virtual::makeTypeIdInstance( location, typeid_type ) );
429 // If the typeid_type is going to be kept, the other reference will have been made by now, but
430 // we also get to avoid extra mutates.
431 ast::ptr<ast::StructInstType> typeid_cleanup = typeid_type;
432}
433
434void ConcurrentSueKeyword::addVtableForward( const ast::StructDecl * decl ) {
435 assert( vtable_decl );
436 const CodeLocation& location = decl->location;
437
438 std::vector<ast::ptr<ast::Expr>> poly_args = {
439 new ast::TypeExpr( location, new ast::StructInstType( decl ) ),
440 };
441 declsToAddBefore.push_back( Virtual::makeGetExceptionForward(
442 location,
443 new ast::StructInstType( vtable_decl, copy( poly_args ) ),
444 new ast::StructInstType( except_decl, copy( poly_args ) )
445 ) );
446 ast::ObjectDecl * vtable_object = Virtual::makeVtableForward(
447 location,
448 "_default_vtable_object_declaration",
449 new ast::StructInstType( vtable_decl, std::move( poly_args ) )
450 );
451 declsToAddBefore.push_back( vtable_object );
452 declsToAddBefore.push_back(
453 new ast::ObjectDecl(
454 location,
455 Virtual::concurrentDefaultVTableName(),
456 new ast::ReferenceType( vtable_object->type, ast::CV::Const ),
457 nullptr,
458 ast::Storage::Extern,
459 ast::Linkage::Cforall
460 )
461 );
462}
463
464const ast::FunctionDecl * ConcurrentSueKeyword::forwardDeclare(
465 const ast::StructDecl * decl ) {
466 const CodeLocation & location = decl->location;
467
468 ast::StructDecl * forward = ast::deepCopy( decl );
469 {
470 // If removing members makes ref-count go to zero, do not free.
471 ast::ptr<ast::StructDecl> forward_ptr = forward;
472 forward->body = false;
473 forward->members.clear();
474 forward_ptr.release();
475 }
476
477 ast::ObjectDecl * this_decl = new ast::ObjectDecl(
478 location,
479 "this",
480 new ast::ReferenceType( new ast::StructInstType( decl ) )
481 );
482
483 ast::ObjectDecl * ret_decl = new ast::ObjectDecl(
484 location,
485 "ret",
486 new ast::PointerType( new ast::StructInstType( type_decl ) )
487 );
488
489 ast::FunctionDecl * get_decl = new ast::FunctionDecl(
490 location,
491 getter_name,
492 { this_decl }, // params
493 { ret_decl }, // returns
494 nullptr, // stmts
495 ast::Storage::Static,
496 ast::Linkage::Cforall,
497 { new ast::Attribute( "const" ) },
498 ast::Function::Inline
499 );
500 get_decl = fixupGenerics( get_decl, decl );
501
502 ast::FunctionDecl * main_decl = nullptr;
503 if ( needs_main ) {
504 // `this_decl` is copied here because the original was used above.
505 main_decl = new ast::FunctionDecl(
506 location,
507 "main",
508 { ast::deepCopy( this_decl ) },
509 {},
510 nullptr,
511 ast::Storage::Classes(),
512 ast::Linkage::Cforall
513 );
514 main_decl = fixupGenerics( main_decl, decl );
515 }
516
517 declsToAddBefore.push_back( forward );
518 if ( needs_main ) declsToAddBefore.push_back( main_decl );
519 declsToAddBefore.push_back( get_decl );
520
521 return get_decl;
522}
523
524ConcurrentSueKeyword::StructAndField ConcurrentSueKeyword::addField(
525 const ast::StructDecl * decl ) {
526 const CodeLocation & location = decl->location;
527
528 ast::ObjectDecl * field = new ast::ObjectDecl(
529 location,
530 field_name,
531 new ast::StructInstType( type_decl )
532 );
533
534 auto mutDecl = ast::mutate( decl );
535 // Insert at start of special aggregate structures => front of vector
536 //mutDecl->members.insert( mutDecl->members.begin(), field );
537 mutDecl->members.push_back( field );
538
539 return {mutDecl, field};
540}
541
542void ConcurrentSueKeyword::addGetRoutines(
543 const ast::ObjectDecl * field, const ast::FunctionDecl * forward ) {
544 // Clone the signature and then build the body.
545 ast::FunctionDecl * decl = ast::deepCopy( forward );
546
547 // Say it is generated at the "same" places as the forward declaration.
548 const CodeLocation & location = decl->location;
549
550 const ast::DeclWithType * param = decl->params.front();
551 ast::Stmt * stmt = new ast::ReturnStmt( location,
552 new ast::AddressExpr( location,
553 new ast::MemberExpr( location,
554 field,
555 new ast::CastExpr( location,
556 new ast::VariableExpr( location, param ),
557 ast::deepCopy( param->get_type()->stripReferences() ),
558 ast::ExplicitCast
559 )
560 )
561 )
562 );
563
564 decl->stmts = new ast::CompoundStmt( location, { stmt } );
565 declsToAddAfter.push_back( decl );
566}
567
568void ConcurrentSueKeyword::addLockUnlockRoutines(
569 const ast::StructDecl * decl ) {
570 // This should only be used on monitors.
571 assert( ast::AggregateDecl::Monitor == cast_target );
572
573 const CodeLocation & location = decl->location;
574
575 // The parameter for both routines.
576 ast::ObjectDecl * this_decl = new ast::ObjectDecl(
577 location,
578 "this",
579 new ast::ReferenceType( new ast::StructInstType( decl ) )
580 );
581
582 ast::FunctionDecl * lock_decl = new ast::FunctionDecl(
583 location,
584 "lock",
585 {
586 // Copy the declaration of this.
587 ast::deepCopy( this_decl ),
588 },
589 { /* returns */ },
590 nullptr,
591 ast::Storage::Static,
592 ast::Linkage::Cforall,
593 { /* attributes */ },
594 ast::Function::Inline
595 );
596 lock_decl = fixupGenerics( lock_decl, decl );
597
598 lock_decl->stmts = new ast::CompoundStmt( location, {
599 new ast::ExprStmt( location,
600 new ast::UntypedExpr( location,
601 new ast::NameExpr( location, "lock" ),
602 {
603 new ast::UntypedExpr( location,
604 new ast::NameExpr( location, "get_monitor" ),
605 { new ast::VariableExpr( location,
606 InitTweak::getParamThis( lock_decl ) ) }
607 )
608 }
609 )
610 )
611 } );
612
613 ast::FunctionDecl * unlock_decl = new ast::FunctionDecl(
614 location,
615 "unlock",
616 {
617 // Last use, consume the declaration of this.
618 this_decl,
619 },
620 { /* returns */ },
621 nullptr,
622 ast::Storage::Static,
623 ast::Linkage::Cforall,
624 { /* attributes */ },
625 ast::Function::Inline
626 );
627 unlock_decl = fixupGenerics( unlock_decl, decl );
628
629 unlock_decl->stmts = new ast::CompoundStmt( location, {
630 new ast::ExprStmt( location,
631 new ast::UntypedExpr( location,
632 new ast::NameExpr( location, "unlock" ),
633 {
634 new ast::UntypedExpr( location,
635 new ast::NameExpr( location, "get_monitor" ),
636 { new ast::VariableExpr( location,
637 InitTweak::getParamThis( unlock_decl ) ) }
638 )
639 }
640 )
641 )
642 } );
643
644 declsToAddAfter.push_back( lock_decl );
645 declsToAddAfter.push_back( unlock_decl );
646}
647
648
649// --------------------------------------------------------------------------
650struct SuspendKeyword final :
651 public ast::WithStmtsToAdd, public ast::WithGuards {
652 SuspendKeyword() = default;
653 virtual ~SuspendKeyword() = default;
654
655 void previsit( const ast::FunctionDecl * );
656 const ast::DeclWithType * postvisit( const ast::FunctionDecl * );
657 const ast::Stmt * postvisit( const ast::SuspendStmt * );
658
659private:
660 bool is_real_suspend( const ast::FunctionDecl * );
661
662 const ast::Stmt * make_generator_suspend( const ast::SuspendStmt * );
663 const ast::Stmt * make_coroutine_suspend( const ast::SuspendStmt * );
664
665 struct LabelPair {
666 ast::Label obj;
667 int idx;
668 };
669
670 LabelPair make_label(const ast::Stmt * stmt ) {
671 labels.push_back( ControlStruct::newLabel( "generator", stmt ) );
672 return { labels.back(), int(labels.size()) };
673 }
674
675 const ast::DeclWithType * in_generator = nullptr;
676 const ast::FunctionDecl * decl_suspend = nullptr;
677 std::vector<ast::Label> labels;
678};
679
680void SuspendKeyword::previsit( const ast::FunctionDecl * decl ) {
681 GuardValue( in_generator ); in_generator = nullptr;
682
683 // If it is the real suspend, grab it if we don't have one already.
684 if ( is_real_suspend( decl ) ) {
685 decl_suspend = decl_suspend ? decl_suspend : decl;
686 return;
687 }
688
689 // Otherwise check if this is a generator main and, if so, handle it.
690 auto param = isMainFor( decl, ast::AggregateDecl::Generator );
691 if ( !param ) return;
692
693 if ( 0 != decl->returns.size() ) {
694 SemanticError( decl->location, "Generator main must return void." );
695 }
696
697 in_generator = param;
698 GuardValue( labels ); labels.clear();
699}
700
701const ast::DeclWithType * SuspendKeyword::postvisit(
702 const ast::FunctionDecl * decl ) {
703 // Only modify a full definition of a generator with states.
704 if ( !decl->stmts || !in_generator || labels.empty() ) return decl;
705
706 const CodeLocation & location = decl->location;
707
708 // Create a new function body:
709 // static void * __generator_labels[] = {&&s0, &&s1, ...};
710 // void * __generator_label = __generator_labels[GEN.__generator_state];
711 // goto * __generator_label;
712 // s0: ;
713 // OLD_BODY
714
715 // This is the null statement inserted right before the body.
716 ast::NullStmt * noop = new ast::NullStmt( location );
717 noop->labels.push_back( ControlStruct::newLabel( "generator", noop ) );
718 const ast::Label & first_label = noop->labels.back();
719
720 // Add each label to the init, starting with the first label.
721 std::vector<ast::ptr<ast::Init>> inits = {
722 new ast::SingleInit( location,
723 new ast::LabelAddressExpr( location, copy( first_label ) ) ) };
724 // Then go through all the stored labels, and clear the store.
725 for ( auto && label : labels ) {
726 inits.push_back( new ast::SingleInit( label.location,
727 new ast::LabelAddressExpr( label.location, std::move( label )
728 ) ) );
729 }
730 labels.clear();
731 // Then construct the initializer itself.
732 auto init = new ast::ListInit( location, std::move( inits ) );
733
734 ast::ObjectDecl * generatorLabels = new ast::ObjectDecl(
735 location,
736 "__generator_labels",
737 new ast::ArrayType(
738 new ast::PointerType( new ast::VoidType() ),
739 nullptr,
740 ast::FixedLen,
741 ast::DynamicDim
742 ),
743 init,
744 ast::Storage::Classes(),
745 ast::Linkage::AutoGen
746 );
747
748 ast::ObjectDecl * generatorLabel = new ast::ObjectDecl(
749 location,
750 "__generator_label",
751 new ast::PointerType( new ast::VoidType() ),
752 new ast::SingleInit( location,
753 new ast::UntypedExpr( location,
754 new ast::NameExpr( location, "?[?]" ),
755 {
756 // TODO: Could be a variable expr.
757 new ast::NameExpr( location, "__generator_labels" ),
758 new ast::UntypedMemberExpr( location,
759 new ast::NameExpr( location, "__generator_state" ),
760 new ast::VariableExpr( location, in_generator )
761 )
762 }
763 )
764 ),
765 ast::Storage::Classes(),
766 ast::Linkage::AutoGen
767 );
768
769 ast::BranchStmt * theGoTo = new ast::BranchStmt(
770 location, new ast::VariableExpr( location, generatorLabel )
771 );
772
773 // The noop goes here in order.
774
775 ast::CompoundStmt * body = new ast::CompoundStmt( location, {
776 { new ast::DeclStmt( location, generatorLabels ) },
777 { new ast::DeclStmt( location, generatorLabel ) },
778 { theGoTo },
779 { noop },
780 { decl->stmts },
781 } );
782
783 auto mutDecl = ast::mutate( decl );
784 mutDecl->stmts = body;
785 return mutDecl;
786}
787
788const ast::Stmt * SuspendKeyword::postvisit( const ast::SuspendStmt * stmt ) {
789 switch ( stmt->kind ) {
790 case ast::SuspendStmt::None:
791 // Use the context to determain the implicit target.
792 if ( in_generator ) {
793 return make_generator_suspend( stmt );
794 } else {
795 return make_coroutine_suspend( stmt );
796 }
797 case ast::SuspendStmt::Coroutine:
798 return make_coroutine_suspend( stmt );
799 case ast::SuspendStmt::Generator:
800 // Generator suspends must be directly in a generator.
801 if ( !in_generator ) SemanticError( stmt->location, "\"suspend generator\" must be used inside main of generator type." );
802 return make_generator_suspend( stmt );
803 }
804 assert( false );
805 return stmt;
806}
807
808/// Find the real/official suspend declaration.
809bool SuspendKeyword::is_real_suspend( const ast::FunctionDecl * decl ) {
810 return ( !decl->linkage.is_mangled
811 && 0 == decl->params.size()
812 && 0 == decl->returns.size()
813 && "__cfactx_suspend" == decl->name );
814}
815
816const ast::Stmt * SuspendKeyword::make_generator_suspend(
817 const ast::SuspendStmt * stmt ) {
818 assert( in_generator );
819 // Target code is:
820 // GEN.__generator_state = X;
821 // THEN
822 // return;
823 // __gen_X:;
824
825 const CodeLocation & location = stmt->location;
826
827 LabelPair label = make_label( stmt );
828
829 // This is the context saving statement.
830 stmtsToAddBefore.push_back( new ast::ExprStmt( location,
831 new ast::UntypedExpr( location,
832 new ast::NameExpr( location, "?=?" ),
833 {
834 new ast::UntypedMemberExpr( location,
835 new ast::NameExpr( location, "__generator_state" ),
836 new ast::VariableExpr( location, in_generator )
837 ),
838 ast::ConstantExpr::from_int( location, label.idx ),
839 }
840 )
841 ) );
842
843 // The THEN component is conditional (return is not).
844 if ( stmt->then ) {
845 stmtsToAddBefore.push_back( stmt->then.get() );
846 }
847 stmtsToAddBefore.push_back( new ast::ReturnStmt( location, nullptr ) );
848
849 // The null statement replaces the old suspend statement.
850 return new ast::NullStmt( location, { label.obj } );
851}
852
853const ast::Stmt * SuspendKeyword::make_coroutine_suspend(
854 const ast::SuspendStmt * stmt ) {
855 // The only thing we need from the old statement is the location.
856 const CodeLocation & location = stmt->location;
857
858 if ( !decl_suspend ) {
859 SemanticError( location, "suspend keyword applied to coroutines requires coroutines to be in scope, add #include <coroutine.hfa>." );
860 }
861 if ( stmt->then ) {
862 SemanticError( location, "Compound statement following coroutines is not implemented." );
863 }
864
865 return new ast::ExprStmt( location,
866 new ast::UntypedExpr( location,
867 ast::VariableExpr::functionPointer( location, decl_suspend ) )
868 );
869}
870
871// --------------------------------------------------------------------------
872struct MutexKeyword final : public ast::WithDeclsToAdd {
873 const ast::FunctionDecl * postvisit( const ast::FunctionDecl * decl );
874 void postvisit( const ast::StructDecl * decl );
875 const ast::Stmt * postvisit( const ast::MutexStmt * stmt );
876
877 static std::vector<const ast::DeclWithType *> findMutexArgs(
878 const ast::FunctionDecl * decl, bool & first );
879 static void validate( const ast::DeclWithType * decl );
880
881 ast::CompoundStmt * addDtorStatements( const ast::FunctionDecl* func, const ast::CompoundStmt *, const std::vector<const ast::DeclWithType *> &);
882 ast::CompoundStmt * addStatements( const ast::FunctionDecl* func, const ast::CompoundStmt *, const std::vector<const ast::DeclWithType *> &);
883 ast::CompoundStmt * addStatements( const ast::CompoundStmt * body, const std::vector<ast::ptr<ast::Expr>> & args );
884 ast::CompoundStmt * addThreadDtorStatements( const ast::FunctionDecl* func, const ast::CompoundStmt * body, const std::vector<const ast::DeclWithType *> & args );
885 ast::ExprStmt * genVirtLockUnlockExpr( const std::string & fnName, ast::ptr<ast::Expr> expr, const CodeLocation & location, ast::Expr * param);
886 ast::IfStmt * genTypeDiscrimLockUnlock( const std::string & fnName, const std::vector<ast::ptr<ast::Expr>> & args, const CodeLocation & location, ast::UntypedExpr * thisParam );
887private:
888 const ast::StructDecl * monitor_decl = nullptr;
889 const ast::StructDecl * guard_decl = nullptr;
890 const ast::StructDecl * dtor_guard_decl = nullptr;
891 const ast::StructDecl * thread_guard_decl = nullptr;
892 const ast::StructDecl * lock_guard_decl = nullptr;
893
894 static ast::ptr<ast::Type> generic_func;
895
896 UniqueName mutex_func_namer = UniqueName("__lock_unlock_curr");
897};
898
899const ast::FunctionDecl * MutexKeyword::postvisit(
900 const ast::FunctionDecl * decl ) {
901 bool is_first_argument_mutex = false;
902 const std::vector<const ast::DeclWithType *> mutexArgs =
903 findMutexArgs( decl, is_first_argument_mutex );
904 bool const isDtor = CodeGen::isDestructor( decl->name );
905
906 // Does this function have any mutex arguments that connect to monitors?
907 if ( mutexArgs.empty() ) {
908 // If this is the destructor for a monitor it must be mutex.
909 if ( isDtor ) {
910 // This reflects MutexKeyword::validate, but no error messages.
911 const ast::Type * type = decl->type->params.front();
912
913 // If it's a copy, it's not a mutex.
914 const ast::ReferenceType * refType = dynamic_cast<const ast::ReferenceType *>( type );
915 if ( nullptr == refType ) {
916 return decl;
917 }
918
919 // If it is not pointing directly to a type, it's not a mutex.
920 auto base = refType->base;
921 if ( base.as<ast::ReferenceType>() ) return decl;
922 if ( base.as<ast::PointerType>() ) return decl;
923
924 // If it is not a struct, it's not a mutex.
925 auto baseStruct = base.as<ast::StructInstType>();
926 if ( nullptr == baseStruct ) return decl;
927
928 // If it is a monitor, then it is a monitor.
929 if ( baseStruct->base->is_monitor() || baseStruct->base->is_thread() ) {
930 SemanticError( decl, "destructors for structures declared as \"monitor\" must use mutex parameters " );
931 }
932 }
933 return decl;
934 }
935
936 // Monitors can't be constructed with mutual exclusion.
937 if ( CodeGen::isConstructor( decl->name ) && is_first_argument_mutex ) {
938 SemanticError( decl, "constructors cannot have mutex parameters " );
939 }
940
941 // It makes no sense to have multiple mutex parameters for the destructor.
942 if ( isDtor && mutexArgs.size() != 1 ) {
943 SemanticError( decl, "destructors can only have 1 mutex argument " );
944 }
945
946 // Make sure all the mutex arguments are monitors.
947 for ( auto arg : mutexArgs ) {
948 validate( arg );
949 }
950
951 // Check to see if the body needs to be instrument the body.
952 const ast::CompoundStmt * body = decl->stmts;
953 if ( !body ) return decl;
954
955 // Check to if the required headers have been seen.
956 if ( !monitor_decl || !guard_decl || !dtor_guard_decl ) {
957 SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor.hfa>." );
958 }
959
960 // Instrument the body.
961 ast::CompoundStmt * newBody = nullptr;
962 if ( isDtor && isThread( mutexArgs.front() ) ) {
963 if ( !thread_guard_decl ) {
964 SemanticError( decl, "thread destructor requires threads to be in scope, add #include <thread.hfa>." );
965 }
966 newBody = addThreadDtorStatements( decl, body, mutexArgs );
967 } else if ( isDtor ) {
968 newBody = addDtorStatements( decl, body, mutexArgs );
969 } else {
970 newBody = addStatements( decl, body, mutexArgs );
971 }
972 assert( newBody );
973 return ast::mutate_field( decl, &ast::FunctionDecl::stmts, newBody );
974}
975
976void MutexKeyword::postvisit( const ast::StructDecl * decl ) {
977 if ( !decl->body ) {
978 return;
979 } else if ( decl->name == "monitor$" ) {
980 assert( !monitor_decl );
981 monitor_decl = decl;
982 } else if ( decl->name == "monitor_guard_t" ) {
983 assert( !guard_decl );
984 guard_decl = decl;
985 } else if ( decl->name == "monitor_dtor_guard_t" ) {
986 assert( !dtor_guard_decl );
987 dtor_guard_decl = decl;
988 } else if ( decl->name == "thread_dtor_guard_t" ) {
989 assert( !thread_guard_decl );
990 thread_guard_decl = decl;
991 } else if ( decl->name == "__mutex_stmt_lock_guard" ) {
992 assert( !lock_guard_decl );
993 lock_guard_decl = decl;
994 }
995}
996
997const ast::Stmt * MutexKeyword::postvisit( const ast::MutexStmt * stmt ) {
998 if ( !lock_guard_decl ) {
999 SemanticError( stmt->location, "mutex stmt requires a header, add #include <mutex_stmt.hfa>." );
1000 }
1001 ast::CompoundStmt * body =
1002 new ast::CompoundStmt( stmt->location, { stmt->stmt } );
1003
1004 return addStatements( body, stmt->mutexObjs );;
1005}
1006
1007std::vector<const ast::DeclWithType *> MutexKeyword::findMutexArgs(
1008 const ast::FunctionDecl * decl, bool & first ) {
1009 std::vector<const ast::DeclWithType *> mutexArgs;
1010
1011 bool once = true;
1012 for ( auto arg : decl->params ) {
1013 const ast::Type * type = arg->get_type();
1014 if ( type->is_mutex() ) {
1015 if ( once ) first = true;
1016 mutexArgs.push_back( arg.get() );
1017 }
1018 once = false;
1019 }
1020 return mutexArgs;
1021}
1022
1023void MutexKeyword::validate( const ast::DeclWithType * decl ) {
1024 const ast::Type * type = decl->get_type();
1025
1026 // If it's a copy, it's not a mutex.
1027 const ast::ReferenceType * refType = dynamic_cast<const ast::ReferenceType *>( type );
1028 if ( nullptr == refType ) {
1029 SemanticError( decl, "Mutex argument must be of reference type " );
1030 }
1031
1032 // If it is not pointing directly to a type, it's not a mutex.
1033 auto base = refType->base;
1034 if ( base.as<ast::ReferenceType>() || base.as<ast::PointerType>() ) {
1035 SemanticError( decl, "Mutex argument have exactly one level of indirection " );
1036 }
1037
1038 // If it is not a struct, it's not a mutex.
1039 auto baseStruct = base.as<ast::StructInstType>();
1040 if ( nullptr == baseStruct ) return;
1041
1042 // Make sure that only the outer reference is mutex.
1043 if ( baseStruct->is_mutex() ) {
1044 SemanticError( decl, "mutex keyword may only appear once per argument " );
1045 }
1046}
1047
1048ast::CompoundStmt * MutexKeyword::addDtorStatements(
1049 const ast::FunctionDecl* func, const ast::CompoundStmt * body,
1050 const std::vector<const ast::DeclWithType *> & args ) {
1051 ast::Type * argType = ast::shallowCopy( args.front()->get_type() );
1052 argType->set_mutex( false );
1053
1054 ast::CompoundStmt * mutBody = ast::mutate( body );
1055
1056 // Generated code goes near the beginning of body:
1057 const CodeLocation & location = mutBody->location;
1058
1059 const ast::ObjectDecl * monitor = new ast::ObjectDecl(
1060 location,
1061 "__monitor",
1062 new ast::PointerType( new ast::StructInstType( monitor_decl ) ),
1063 new ast::SingleInit(
1064 location,
1065 new ast::UntypedExpr(
1066 location,
1067 new ast::NameExpr( location, "get_monitor" ),
1068 { new ast::CastExpr(
1069 location,
1070 new ast::VariableExpr( location, args.front() ),
1071 argType, ast::ExplicitCast
1072 ) }
1073 )
1074 )
1075 );
1076
1077 assert( generic_func );
1078
1079 // In reverse order:
1080 // monitor_dtor_guard_t __guard = { __monitor, func, false };
1081 mutBody->push_front(
1082 new ast::DeclStmt( location, new ast::ObjectDecl(
1083 location,
1084 "__guard",
1085 new ast::StructInstType( dtor_guard_decl ),
1086 new ast::ListInit(
1087 location,
1088 {
1089 new ast::SingleInit( location,
1090 new ast::AddressExpr( location,
1091 new ast::VariableExpr( location, monitor ) ) ),
1092 new ast::SingleInit( location,
1093 new ast::CastExpr( location,
1094 new ast::VariableExpr( location, func ),
1095 generic_func,
1096 ast::ExplicitCast ) ),
1097 new ast::SingleInit( location,
1098 ast::ConstantExpr::from_bool( location, false ) ),
1099 },
1100 {},
1101 ast::MaybeConstruct
1102 )
1103 ))
1104 );
1105
1106 // monitor$ * __monitor = get_monitor(a);
1107 mutBody->push_front( new ast::DeclStmt( location, monitor ) );
1108
1109 return mutBody;
1110}
1111
1112ast::CompoundStmt * MutexKeyword::addStatements(
1113 const ast::FunctionDecl* func, const ast::CompoundStmt * body,
1114 const std::vector<const ast::DeclWithType * > & args ) {
1115 ast::CompoundStmt * mutBody = ast::mutate( body );
1116
1117 // Code is generated near the beginning of the compound statement.
1118 const CodeLocation & location = mutBody->location;
1119
1120 // Make pointer to the monitors.
1121 ast::ObjectDecl * monitors = new ast::ObjectDecl(
1122 location,
1123 "__monitors",
1124 new ast::ArrayType(
1125 new ast::PointerType(
1126 new ast::StructInstType( monitor_decl )
1127 ),
1128 ast::ConstantExpr::from_ulong( location, args.size() ),
1129 ast::FixedLen,
1130 ast::DynamicDim
1131 ),
1132 new ast::ListInit(
1133 location,
1134 map_range<std::vector<ast::ptr<ast::Init>>>(
1135 args,
1136 []( const ast::DeclWithType * decl ) {
1137 return new ast::SingleInit(
1138 decl->location,
1139 new ast::UntypedExpr(
1140 decl->location,
1141 new ast::NameExpr( decl->location, "get_monitor" ),
1142 {
1143 new ast::CastExpr(
1144 decl->location,
1145 new ast::VariableExpr( decl->location, decl ),
1146 decl->get_type(),
1147 ast::ExplicitCast
1148 )
1149 }
1150 )
1151 );
1152 }
1153 )
1154 )
1155 );
1156
1157 assert( generic_func );
1158
1159 // In Reverse Order:
1160 mutBody->push_front(
1161 new ast::DeclStmt( location, new ast::ObjectDecl(
1162 location,
1163 "__guard",
1164 new ast::StructInstType( guard_decl ),
1165 new ast::ListInit(
1166 location,
1167 {
1168 new ast::SingleInit( location,
1169 new ast::VariableExpr( location, monitors ) ),
1170 new ast::SingleInit( location,
1171 ast::ConstantExpr::from_ulong( location, args.size() ) ),
1172 new ast::SingleInit( location, new ast::CastExpr(
1173 location,
1174 new ast::VariableExpr( location, func ),
1175 generic_func,
1176 ast::ExplicitCast
1177 ) ),
1178 },
1179 {},
1180 ast::MaybeConstruct
1181 )
1182 ))
1183 );
1184
1185 // monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
1186 mutBody->push_front( new ast::DeclStmt( location, monitors ) );
1187
1188 return mutBody;
1189}
1190
1191// Generates a cast to the void ptr to the appropriate lock type and dereferences it before calling
1192// lock or unlock on it used to undo the type erasure done by storing all the lock pointers as void.
1193ast::ExprStmt * MutexKeyword::genVirtLockUnlockExpr( const std::string & fnName, ast::ptr<ast::Expr> expr, const CodeLocation & location, ast::Expr * param ) {
1194 return new ast::ExprStmt( location,
1195 new ast::UntypedExpr( location,
1196 new ast::NameExpr( location, fnName ), {
1197 ast::UntypedExpr::createDeref(
1198 location,
1199 new ast::CastExpr( location,
1200 param,
1201 new ast::PointerType( new ast::TypeofType( new ast::UntypedExpr(
1202 expr->location,
1203 new ast::NameExpr( expr->location, "__get_mutexstmt_lock_type" ),
1204 { expr }
1205 ) ) ),
1206 ast::GeneratedFlag::ExplicitCast
1207 )
1208 )
1209 }
1210 )
1211 );
1212}
1213
1214ast::IfStmt * MutexKeyword::genTypeDiscrimLockUnlock( const std::string & fnName, const std::vector<ast::ptr<ast::Expr>> & args, const CodeLocation & location, ast::UntypedExpr * thisParam ) {
1215 ast::IfStmt * outerLockIf = nullptr;
1216 ast::IfStmt * lastLockIf = nullptr;
1217
1218 //adds an if/elif clause for each lock to assign type from void ptr based on ptr address
1219 for ( long unsigned int i = 0; i < args.size(); i++ ) {
1220
1221 ast::UntypedExpr * ifCond = new ast::UntypedExpr( location,
1222 new ast::NameExpr( location, "?==?" ), {
1223 ast::deepCopy( thisParam ),
1224 new ast::CastExpr( location, new ast::AddressExpr( location, args.at(i) ), new ast::PointerType( new ast::VoidType() ))
1225 }
1226 );
1227
1228 ast::IfStmt * currLockIf = new ast::IfStmt(
1229 location,
1230 ifCond,
1231 genVirtLockUnlockExpr( fnName, args.at(i), location, ast::deepCopy( thisParam ) )
1232 );
1233
1234 if ( i == 0 ) {
1235 outerLockIf = currLockIf;
1236 } else {
1237 // add ifstmt to else of previous stmt
1238 lastLockIf->else_ = currLockIf;
1239 }
1240
1241 lastLockIf = currLockIf;
1242 }
1243 return outerLockIf;
1244}
1245
1246void flattenTuple( const ast::UntypedTupleExpr * tuple, std::vector<ast::ptr<ast::Expr>> & output ) {
1247 for ( auto & expr : tuple->exprs ) {
1248 const ast::UntypedTupleExpr * innerTuple = dynamic_cast<const ast::UntypedTupleExpr *>(expr.get());
1249 if ( innerTuple ) flattenTuple( innerTuple, output );
1250 else output.emplace_back( ast::deepCopy( expr ));
1251 }
1252}
1253
1254ast::CompoundStmt * MutexKeyword::addStatements(
1255 const ast::CompoundStmt * body,
1256 const std::vector<ast::ptr<ast::Expr>> & args ) {
1257
1258 // Code is generated near the beginning of the compound statement.
1259 const CodeLocation & location = body->location;
1260
1261 // final body to return
1262 ast::CompoundStmt * newBody = new ast::CompoundStmt( location );
1263
1264 // std::string lockFnName = mutex_func_namer.newName();
1265 // std::string unlockFnName = mutex_func_namer.newName();
1266
1267 // If any arguments to the mutex stmt are tuples, flatten them
1268 std::vector<ast::ptr<ast::Expr>> flattenedArgs;
1269 for ( auto & arg : args ) {
1270 const ast::UntypedTupleExpr * tuple = dynamic_cast<const ast::UntypedTupleExpr *>(args.at(0).get());
1271 if ( tuple ) flattenTuple( tuple, flattenedArgs );
1272 else flattenedArgs.emplace_back( ast::deepCopy( arg ));
1273 }
1274
1275 // Make pointer to the monitors.
1276 ast::ObjectDecl * monitors = new ast::ObjectDecl(
1277 location,
1278 "__monitors",
1279 new ast::ArrayType(
1280 new ast::PointerType(
1281 new ast::VoidType()
1282 ),
1283 ast::ConstantExpr::from_ulong( location, flattenedArgs.size() ),
1284 ast::FixedLen,
1285 ast::DynamicDim
1286 ),
1287 new ast::ListInit(
1288 location,
1289 map_range<std::vector<ast::ptr<ast::Init>>>(
1290 flattenedArgs, [](const ast::Expr * expr) {
1291 return new ast::SingleInit(
1292 expr->location,
1293 new ast::UntypedExpr(
1294 expr->location,
1295 new ast::NameExpr( expr->location, "__get_mutexstmt_lock_ptr" ),
1296 { expr }
1297 )
1298 );
1299 }
1300 )
1301 )
1302 );
1303
1304 ast::StructInstType * lock_guard_struct =
1305 new ast::StructInstType( lock_guard_decl );
1306
1307 // use try stmts to lock and finally to unlock
1308 ast::TryStmt * outerTry = nullptr;
1309 ast::TryStmt * currentTry;
1310 ast::CompoundStmt * lastBody = nullptr;
1311
1312 // adds a nested try stmt for each lock we are locking
1313 for ( long unsigned int i = 0; i < flattenedArgs.size(); i++ ) {
1314 ast::UntypedExpr * innerAccess = new ast::UntypedExpr(
1315 location,
1316 new ast::NameExpr( location,"?[?]" ), {
1317 new ast::NameExpr( location, "__monitors" ),
1318 ast::ConstantExpr::from_int( location, i )
1319 }
1320 );
1321
1322 // make the try body
1323 ast::CompoundStmt * currTryBody = new ast::CompoundStmt( location );
1324 ast::IfStmt * lockCall = genTypeDiscrimLockUnlock( "lock", flattenedArgs, location, innerAccess );
1325 currTryBody->push_back( lockCall );
1326
1327 // make the finally stmt
1328 ast::CompoundStmt * currFinallyBody = new ast::CompoundStmt( location );
1329 ast::IfStmt * unlockCall = genTypeDiscrimLockUnlock( "unlock", flattenedArgs, location, innerAccess );
1330 currFinallyBody->push_back( unlockCall );
1331
1332 // construct the current try
1333 currentTry = new ast::TryStmt(
1334 location,
1335 currTryBody,
1336 {},
1337 new ast::FinallyClause( location, currFinallyBody )
1338 );
1339 if ( i == 0 ) outerTry = currentTry;
1340 else {
1341 // pushback try into the body of the outer try
1342 lastBody->push_back( currentTry );
1343 }
1344 lastBody = currTryBody;
1345 }
1346
1347 // push body into innermost try body
1348 if ( lastBody != nullptr ) {
1349 lastBody->push_back( body );
1350 newBody->push_front( outerTry );
1351 }
1352
1353 // monitor_guard_t __guard = { __monitors, # };
1354 newBody->push_front(
1355 new ast::DeclStmt(
1356 location,
1357 new ast::ObjectDecl(
1358 location,
1359 "__guard",
1360 lock_guard_struct,
1361 new ast::ListInit(
1362 location,
1363 {
1364 new ast::SingleInit(
1365 location,
1366 new ast::VariableExpr( location, monitors ) ),
1367 new ast::SingleInit(
1368 location,
1369 ast::ConstantExpr::from_ulong( location, flattenedArgs.size() ) ),
1370 },
1371 {},
1372 ast::MaybeConstruct
1373 )
1374 )
1375 )
1376 );
1377
1378 // monitor$ * __monitors[] = { get_monitor(a), get_monitor(b) };
1379 newBody->push_front( new ast::DeclStmt( location, monitors ) );
1380
1381 // // The parameter for both __lock_curr/__unlock_curr routines.
1382 // ast::ObjectDecl * this_decl = new ast::ObjectDecl(
1383 // location,
1384 // "this",
1385 // new ast::PointerType( new ast::VoidType() ),
1386 // nullptr,
1387 // {},
1388 // ast::Linkage::Cforall
1389 // );
1390
1391 // ast::FunctionDecl * lock_decl = new ast::FunctionDecl(
1392 // location,
1393 // lockFnName,
1394 // { /* forall */ },
1395 // {
1396 // // Copy the declaration of this.
1397 // this_decl,
1398 // },
1399 // { /* returns */ },
1400 // nullptr,
1401 // 0,
1402 // ast::Linkage::Cforall,
1403 // { /* attributes */ },
1404 // ast::Function::Inline
1405 // );
1406
1407 // ast::FunctionDecl * unlock_decl = new ast::FunctionDecl(
1408 // location,
1409 // unlockFnName,
1410 // { /* forall */ },
1411 // {
1412 // // Copy the declaration of this.
1413 // ast::deepCopy( this_decl ),
1414 // },
1415 // { /* returns */ },
1416 // nullptr,
1417 // 0,
1418 // ast::Linkage::Cforall,
1419 // { /* attributes */ },
1420 // ast::Function::Inline
1421 // );
1422
1423 // ast::IfStmt * outerLockIf = nullptr;
1424 // ast::IfStmt * outerUnlockIf = nullptr;
1425 // ast::IfStmt * lastLockIf = nullptr;
1426 // ast::IfStmt * lastUnlockIf = nullptr;
1427
1428 // //adds an if/elif clause for each lock to assign type from void ptr based on ptr address
1429 // for ( long unsigned int i = 0; i < args.size(); i++ ) {
1430 // ast::VariableExpr * thisParam = new ast::VariableExpr( location, InitTweak::getParamThis( lock_decl ) );
1431 // ast::UntypedExpr * ifCond = new ast::UntypedExpr( location,
1432 // new ast::NameExpr( location, "?==?" ), {
1433 // thisParam,
1434 // new ast::CastExpr( location, new ast::AddressExpr( location, args.at(i) ), new ast::PointerType( new ast::VoidType() ))
1435 // }
1436 // );
1437
1438 // ast::IfStmt * currLockIf = new ast::IfStmt(
1439 // location,
1440 // ast::deepCopy( ifCond ),
1441 // genVirtLockUnlockExpr( "lock", args.at(i), location, ast::deepCopy( thisParam ) )
1442 // );
1443
1444 // ast::IfStmt * currUnlockIf = new ast::IfStmt(
1445 // location,
1446 // ifCond,
1447 // genVirtLockUnlockExpr( "unlock", args.at(i), location, ast::deepCopy( thisParam ) )
1448 // );
1449
1450 // if ( i == 0 ) {
1451 // outerLockIf = currLockIf;
1452 // outerUnlockIf = currUnlockIf;
1453 // } else {
1454 // // add ifstmt to else of previous stmt
1455 // lastLockIf->else_ = currLockIf;
1456 // lastUnlockIf->else_ = currUnlockIf;
1457 // }
1458
1459 // lastLockIf = currLockIf;
1460 // lastUnlockIf = currUnlockIf;
1461 // }
1462
1463 // // add pointer typing if/elifs to body of routines
1464 // lock_decl->stmts = new ast::CompoundStmt( location, { outerLockIf } );
1465 // unlock_decl->stmts = new ast::CompoundStmt( location, { outerUnlockIf } );
1466
1467 // // add routines to scope
1468 // declsToAddBefore.push_back( lock_decl );
1469 // declsToAddBefore.push_back( unlock_decl );
1470
1471 // newBody->push_front(new ast::DeclStmt( location, lock_decl ));
1472 // newBody->push_front(new ast::DeclStmt( location, unlock_decl ));
1473
1474 return newBody;
1475}
1476
1477ast::CompoundStmt * MutexKeyword::addThreadDtorStatements(
1478 const ast::FunctionDecl*, const ast::CompoundStmt * body,
1479 const std::vector<const ast::DeclWithType * > & args ) {
1480 assert( args.size() == 1 );
1481 const ast::DeclWithType * arg = args.front();
1482 const ast::Type * argType = arg->get_type();
1483 assert( argType->is_mutex() );
1484
1485 ast::CompoundStmt * mutBody = ast::mutate( body );
1486
1487 // The code is generated near the front of the body.
1488 const CodeLocation & location = mutBody->location;
1489
1490 // thread_dtor_guard_t __guard = { this, intptr( 0 ) };
1491 mutBody->push_front( new ast::DeclStmt(
1492 location,
1493 new ast::ObjectDecl(
1494 location,
1495 "__guard",
1496 new ast::StructInstType( thread_guard_decl ),
1497 new ast::ListInit(
1498 location,
1499 {
1500 new ast::SingleInit( location,
1501 new ast::CastExpr( location,
1502 new ast::VariableExpr( location, arg ), argType ) ),
1503 new ast::SingleInit(
1504 location,
1505 new ast::UntypedExpr(
1506 location,
1507 new ast::NameExpr( location, "intptr" ), {
1508 ast::ConstantExpr::from_int( location, 0 ),
1509 }
1510 ) ),
1511 },
1512 {},
1513 ast::MaybeConstruct
1514 )
1515 )
1516 ));
1517
1518 return mutBody;
1519}
1520
1521ast::ptr<ast::Type> MutexKeyword::generic_func =
1522 new ast::FunctionType( ast::FixedArgs );
1523
1524// --------------------------------------------------------------------------
1525struct ThreadStarter final {
1526 void previsit( const ast::StructDecl * decl );
1527 const ast::FunctionDecl * postvisit( const ast::FunctionDecl * decl );
1528
1529private:
1530 bool thread_ctor_seen = false;
1531 const ast::StructDecl * thread_decl = nullptr;
1532};
1533
1534void ThreadStarter::previsit( const ast::StructDecl * decl ) {
1535 if ( decl->body && decl->name == "thread$" ) {
1536 assert( !thread_decl );
1537 thread_decl = decl;
1538 }
1539}
1540
1541const ast::FunctionDecl * ThreadStarter::postvisit( const ast::FunctionDecl * decl ) {
1542 if ( !CodeGen::isConstructor( decl->name ) ) return decl;
1543
1544 // Seach for the thread constructor.
1545 // (Are the "prefixes" of these to blocks the same?)
1546 const ast::Type * typeof_this = InitTweak::getTypeofThis( decl->type );
1547 auto ctored_type = dynamic_cast<const ast::StructInstType *>( typeof_this );
1548 if ( ctored_type && ctored_type->base == thread_decl ) {
1549 thread_ctor_seen = true;
1550 }
1551
1552 // Modify this declaration, the extra checks to see if we will are first.
1553 const ast::ptr<ast::DeclWithType> & param = decl->params.front();
1554 auto type = dynamic_cast<const ast::StructInstType *>(
1555 ast::getPointerBase( param->get_type() ) );
1556 if ( nullptr == type ) return decl;
1557 if ( !type->base->is_thread() ) return decl;
1558 if ( !thread_decl || !thread_ctor_seen ) {
1559 SemanticError( type->base->location, "thread keyword requires threads to be in scope, add #include <thread.hfa>." );
1560 }
1561 const ast::CompoundStmt * stmt = decl->stmts;
1562 if ( nullptr == stmt ) return decl;
1563
1564 // Now do the actual modification:
1565 ast::CompoundStmt * mutStmt = ast::mutate( stmt );
1566 const CodeLocation & location = mutStmt->location;
1567 mutStmt->push_back(
1568 new ast::ExprStmt(
1569 location,
1570 new ast::UntypedExpr(
1571 location,
1572 new ast::NameExpr( location, "__thrd_start" ),
1573 {
1574 new ast::VariableExpr( location, param ),
1575 new ast::NameExpr( location, "main" ),
1576 }
1577 )
1578 )
1579 );
1580
1581 return ast::mutate_field( decl, &ast::FunctionDecl::stmts, mutStmt );
1582}
1583
1584} // namespace
1585
1586// --------------------------------------------------------------------------
1587// Interface Functions:
1588
1589void implementKeywords( ast::TranslationUnit & translationUnit ) {
1590 ast::Pass<ThreadKeyword>::run( translationUnit );
1591 ast::Pass<CoroutineKeyword>::run( translationUnit );
1592 ast::Pass<MonitorKeyword>::run( translationUnit );
1593 ast::Pass<GeneratorKeyword>::run( translationUnit );
1594 ast::Pass<SuspendKeyword>::run( translationUnit );
1595}
1596
1597void implementMutex( ast::TranslationUnit & translationUnit ) {
1598 ast::Pass<MutexKeyword>::run( translationUnit );
1599}
1600
1601void implementThreadStarter( ast::TranslationUnit & translationUnit ) {
1602 ast::Pass<ThreadStarter>::run( translationUnit );
1603}
1604
1605}
1606
1607// Local Variables: //
1608// tab-width: 4 //
1609// mode: c++ //
1610// compile-command: "make install" //
1611// End: //
Note: See TracBrowser for help on using the repository browser.