source: src/Concurrency/Keywords.cc @ b21c77a

new-env
Last change on this file since b21c77a was 28f3a19, checked in by Aaron Moss <a3moss@…>, 6 years ago

Merge branch 'master' into with_gc

  • Property mode set to 100644
File size: 21.9 KB
RevLine 
[64adb03]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.cc --
8//
9// Author           : Thierry Delisle
10// Created On       : Mon Mar 13 12:41:22 2017
11// Last Modified By :
12// Last Modified On :
[6b0b624]13// Update Count     : 5
[64adb03]14//
15
16#include "Concurrency/Keywords.h"
17
[bf2438c]18#include <cassert>                 // for assert
19#include <string>                  // for string, operator==
20
[bd06384]21#include "Common/GC.h"             // for new_static_root
[2065609]22#include "Common/PassVisitor.h"    // for PassVisitor
[bf2438c]23#include "Common/SemanticError.h"  // for SemanticError
24#include "Common/utility.h"        // for deleteAll, map_range
[bff227f]25#include "CodeGen/OperatorTable.h" // for isConstructor
26#include "InitTweak/InitTweak.h"   // for getPointerBase
[bf2438c]27#include "Parser/LinkageSpec.h"    // for Cforall
28#include "SynTree/Constant.h"      // for Constant
29#include "SynTree/Declaration.h"   // for StructDecl, FunctionDecl, ObjectDecl
30#include "SynTree/Expression.h"    // for VariableExpr, ConstantExpr, Untype...
31#include "SynTree/Initializer.h"   // for SingleInit, ListInit, Initializer ...
32#include "SynTree/Label.h"         // for Label
33#include "SynTree/Statement.h"     // for CompoundStmt, DeclStmt, ExprStmt
34#include "SynTree/Type.h"          // for StructInstType, Type, PointerType
35#include "SynTree/Visitor.h"       // for Visitor, acceptAll
36
37class Attribute;
[64adb03]38
39namespace Concurrency {
40        //=============================================================================================
[2065609]41        // Pass declarations
[64adb03]42        //=============================================================================================
43
[bcda04c]44        //-----------------------------------------------------------------------------
45        //Handles sue type declarations :
46        // sue MyType {                             struct MyType {
47        //      int data;                                  int data;
48        //      a_struct_t more_data;                      a_struct_t more_data;
49        //                                =>             NewField_t newField;
50        // };                                        };
51        //                                           static inline NewField_t * getter_name( MyType * this ) { return &this->newField; }
52        //
[2065609]53        class ConcurrentSueKeyword : public WithDeclsToAdd {
[bcda04c]54          public:
55
[9a705dc8]56                ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error, bool needs_main, KeywordCastExpr::Target cast_target ) :
57                  type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ), needs_main( needs_main ), cast_target( cast_target ) {}
[bcda04c]58
59                virtual ~ConcurrentSueKeyword() {}
60
[9a705dc8]61                Declaration * postmutate( StructDecl * decl );
[bcda04c]62
63                void handle( StructDecl * );
64                FunctionDecl * forwardDeclare( StructDecl * );
65                ObjectDecl * addField( StructDecl * );
[2f9a722]66                void addRoutines( ObjectDecl *, FunctionDecl * );
[bcda04c]67
68                virtual bool is_target( StructDecl * decl ) = 0;
69
[9a705dc8]70                Expression * postmutate( KeywordCastExpr * cast );
71
[bcda04c]72          private:
73                const std::string type_name;
74                const std::string field_name;
75                const std::string getter_name;
76                const std::string context_error;
[bd4d011]77                bool needs_main;
[9a705dc8]78                KeywordCastExpr::Target cast_target;
[bcda04c]79
80                StructDecl* type_decl = nullptr;
81        };
82
83
[64adb03]84        //-----------------------------------------------------------------------------
85        //Handles thread type declarations :
86        // thread Mythread {                         struct MyThread {
87        //      int data;                                  int data;
88        //      a_struct_t more_data;                      a_struct_t more_data;
89        //                                =>             thread_desc __thrd_d;
90        // };                                        };
91        //                                           static inline thread_desc * get_thread( MyThread * this ) { return &this->__thrd_d; }
92        //
[bcda04c]93        class ThreadKeyword final : public ConcurrentSueKeyword {
[64adb03]94          public:
95
[bcda04c]96                ThreadKeyword() : ConcurrentSueKeyword(
97                        "thread_desc",
98                        "__thrd",
99                        "get_thread",
[bd4d011]100                        "thread keyword requires threads to be in scope, add #include <thread>",
[9a705dc8]101                        true,
102                        KeywordCastExpr::Thread
[bcda04c]103                )
104                {}
105
106                virtual ~ThreadKeyword() {}
107
108                virtual bool is_target( StructDecl * decl ) override final { return decl->is_thread(); }
109
110                static void implement( std::list< Declaration * > & translationUnit ) {
[2065609]111                        PassVisitor< ThreadKeyword > impl;
[9a705dc8]112                        mutateAll( translationUnit, impl );
[bcda04c]113                }
[64adb03]114        };
115
116        //-----------------------------------------------------------------------------
117        //Handles coroutine type declarations :
118        // coroutine MyCoroutine {                   struct MyCoroutine {
119        //      int data;                                  int data;
120        //      a_struct_t more_data;                      a_struct_t more_data;
121        //                                =>             coroutine_desc __cor_d;
122        // };                                        };
123        //                                           static inline coroutine_desc * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; }
124        //
[bcda04c]125        class CoroutineKeyword final : public ConcurrentSueKeyword {
[64adb03]126          public:
127
[bcda04c]128                CoroutineKeyword() : ConcurrentSueKeyword(
129                        "coroutine_desc",
130                        "__cor",
131                        "get_coroutine",
[bd4d011]132                        "coroutine keyword requires coroutines to be in scope, add #include <coroutine>",
[9a705dc8]133                        true,
134                        KeywordCastExpr::Coroutine
[bcda04c]135                )
136                {}
[b32ada31]137
[bcda04c]138                virtual ~CoroutineKeyword() {}
139
140                virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
[b32ada31]141
142                static void implement( std::list< Declaration * > & translationUnit ) {
[2065609]143                        PassVisitor< CoroutineKeyword > impl;
[9a705dc8]144                        mutateAll( translationUnit, impl );
[b32ada31]145                }
[64adb03]146        };
147
148        //-----------------------------------------------------------------------------
149        //Handles monitor type declarations :
150        // monitor MyMonitor {                       struct MyMonitor {
151        //      int data;                                  int data;
152        //      a_struct_t more_data;                      a_struct_t more_data;
153        //                                =>             monitor_desc __mon_d;
154        // };                                        };
155        //                                           static inline monitor_desc * get_coroutine( MyMonitor * this ) { return &this->__cor_d; }
156        //
[bcda04c]157        class MonitorKeyword final : public ConcurrentSueKeyword {
[64adb03]158          public:
159
[bcda04c]160                MonitorKeyword() : ConcurrentSueKeyword(
161                        "monitor_desc",
162                        "__mon",
163                        "get_monitor",
[bd4d011]164                        "monitor keyword requires monitors to be in scope, add #include <monitor>",
[9a705dc8]165                        false,
166                        KeywordCastExpr::Monitor
[bcda04c]167                )
168                {}
169
170                virtual ~MonitorKeyword() {}
171
172                virtual bool is_target( StructDecl * decl ) override final { return decl->is_monitor(); }
173
174                static void implement( std::list< Declaration * > & translationUnit ) {
[2065609]175                        PassVisitor< MonitorKeyword > impl;
[9a705dc8]176                        mutateAll( translationUnit, impl );
[bcda04c]177                }
[64adb03]178        };
179
180        //-----------------------------------------------------------------------------
181        //Handles mutex routines definitions :
182        // void foo( A * mutex a, B * mutex b,  int i ) {                  void foo( A * a, B * b,  int i ) {
[9243cc91]183        //                                                                       monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
[64adb03]184        //                                                                       monitor_guard_t __guard = { __monitors, 2 };
185        //    /*Some code*/                                       =>           /*Some code*/
186        // }                                                               }
187        //
[2065609]188        class MutexKeyword final {
[64adb03]189          public:
190
[2065609]191                void postvisit( FunctionDecl * decl );
192                void postvisit(   StructDecl * decl );
[64adb03]193
[db4d8e3]194                std::list<DeclarationWithType*> findMutexArgs( FunctionDecl*, bool & first );
[64adb03]195                void validate( DeclarationWithType * );
[549c006]196                void addDtorStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
[97e3296]197                void addStatments( FunctionDecl* func, CompoundStmt *, const std::list<DeclarationWithType * > &);
[64adb03]198
199                static void implement( std::list< Declaration * > & translationUnit ) {
[2065609]200                        PassVisitor< MutexKeyword > impl;
[64adb03]201                        acceptAll( translationUnit, impl );
202                }
[9243cc91]203
204          private:
205                StructDecl* monitor_decl = nullptr;
[ef42b143]206                StructDecl* guard_decl = nullptr;
[549c006]207                StructDecl* dtor_guard_decl = nullptr;
[97e3296]208
[8d7bef2]209                static Type* generic_func;
[64adb03]210        };
211
[bd06384]212        Type* MutexKeyword::generic_func = new_static_root<FunctionType>( noQualifiers, true );
[97e3296]213
[bd4d011]214        //-----------------------------------------------------------------------------
215        //Handles mutex routines definitions :
216        // void foo( A * mutex a, B * mutex b,  int i ) {                  void foo( A * a, B * b,  int i ) {
217        //                                                                       monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
218        //                                                                       monitor_guard_t __guard = { __monitors, 2 };
219        //    /*Some code*/                                       =>           /*Some code*/
220        // }                                                               }
221        //
[2065609]222        class ThreadStarter final {
[bd4d011]223          public:
224
[2065609]225                void postvisit( FunctionDecl * decl );
[549c006]226                void previsit ( StructDecl   * decl );
[bd4d011]227
228                void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
229
230                static void implement( std::list< Declaration * > & translationUnit ) {
[2065609]231                        PassVisitor< ThreadStarter > impl;
[bd4d011]232                        acceptAll( translationUnit, impl );
233                }
[549c006]234
235          private :
236                bool thread_ctor_seen = false;
237                StructDecl * thread_decl = nullptr;
[bd4d011]238        };
239
[64adb03]240        //=============================================================================================
241        // General entry routine
242        //=============================================================================================
243        void applyKeywords( std::list< Declaration * > & translationUnit ) {
244                ThreadKeyword   ::implement( translationUnit );
245                CoroutineKeyword        ::implement( translationUnit );
246                MonitorKeyword  ::implement( translationUnit );
[bcda04c]247        }
248
249        void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
[64adb03]250                MutexKeyword    ::implement( translationUnit );
251        }
252
[bcda04c]253        void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
[bd4d011]254                ThreadStarter   ::implement( translationUnit );
[bcda04c]255        }
256
[b32ada31]257        //=============================================================================================
[bcda04c]258        // Generic keyword implementation
[b32ada31]259        //=============================================================================================
[2db79e5]260        void fixupGenerics(FunctionType * func, StructDecl * decl) {
261                cloneAll(decl->parameters, func->forall);
262                for ( TypeDecl * td : func->forall ) {
263                        strict_dynamic_cast<StructInstType*>(
264                                func->parameters.front()->get_type()->stripReferences()
265                        )->parameters.push_back(
266                                new TypeExpr( new TypeInstType( noQualifiers, td->name, td ) )
267                        );
268                }
269        }
270
[9a705dc8]271        Declaration * ConcurrentSueKeyword::postmutate(StructDecl * decl) {
[9f5ecf5]272                if( decl->name == type_name && decl->body ) {
[bcda04c]273                        assert( !type_decl );
274                        type_decl = decl;
[b32ada31]275                }
[bcda04c]276                else if ( is_target(decl) ) {
[b32ada31]277                        handle( decl );
278                }
[9a705dc8]279                return decl;
[b32ada31]280        }
281
[9a705dc8]282        Expression * ConcurrentSueKeyword::postmutate( KeywordCastExpr * cast ) {
283                if ( cast_target == cast->target ) {
284                        // convert (thread &)t to (thread_desc &)*get_thread(t), etc.
285                        if( !type_decl ) SemanticError( cast, context_error );
286                        return new CastExpr(
287                                UntypedExpr::createDeref(
[42107b4]288                                        new UntypedExpr( new NameExpr( getter_name ), { cast->arg } )
[9a705dc8]289                                ),
290                                new ReferenceType(
291                                        noQualifiers,
292                                        new StructInstType( noQualifiers, type_decl ) )
293                                );
294                }
295                return cast;
296        }
297
298
[bcda04c]299        void ConcurrentSueKeyword::handle( StructDecl * decl ) {
[9f5ecf5]300                if( ! decl->body ) return;
[b32ada31]301
[a16764a6]302                if( !type_decl ) SemanticError( decl, context_error );
[b32ada31]303
[bcda04c]304                FunctionDecl * func = forwardDeclare( decl );
305                ObjectDecl * field = addField( decl );
[2f9a722]306                addRoutines( field, func );
[b32ada31]307        }
308
[bcda04c]309        FunctionDecl * ConcurrentSueKeyword::forwardDeclare( StructDecl * decl ) {
310
311                StructDecl * forward = decl->clone();
312                forward->set_body( false );
313                forward->get_members().clear();
314
[bd4d011]315                FunctionType * get_type = new FunctionType( noQualifiers, false );
[bcda04c]316                ObjectDecl * this_decl = new ObjectDecl(
317                        "this",
[ba3706f]318                        noStorageClasses,
[b32ada31]319                        LinkageSpec::Cforall,
320                        nullptr,
[83a071f9]321                        new ReferenceType(
[b32ada31]322                                noQualifiers,
[bcda04c]323                                new StructInstType(
324                                        noQualifiers,
325                                        decl
326                                )
[b32ada31]327                        ),
328                        nullptr
329                );
330
[2db79e5]331                get_type->get_parameters().push_back( this_decl->clone() );
[bd4d011]332                get_type->get_returnVals().push_back(
[e04b636]333                        new ObjectDecl(
334                                "ret",
[ba3706f]335                                noStorageClasses,
[e04b636]336                                LinkageSpec::Cforall,
337                                nullptr,
338                                new PointerType(
339                                        noQualifiers,
340                                        new StructInstType(
341                                                noQualifiers,
[bcda04c]342                                                type_decl
[e04b636]343                                        )
344                                ),
345                                nullptr
346                        )
347                );
[2db79e5]348                fixupGenerics(get_type, decl);
[b32ada31]349
[bcda04c]350                FunctionDecl * get_decl = new FunctionDecl(
351                        getter_name,
352                        Type::Static,
353                        LinkageSpec::Cforall,
[bd4d011]354                        get_type,
[bcda04c]355                        nullptr,
356                        noAttributes,
357                        Type::Inline
358                );
359
[bd4d011]360                FunctionDecl * main_decl = nullptr;
361
362                if( needs_main ) {
363                        FunctionType * main_type = new FunctionType( noQualifiers, false );
[bf2438c]364
[bd4d011]365                        main_type->get_parameters().push_back( this_decl->clone() );
366
367                        main_decl = new FunctionDecl(
368                                "main",
[ba3706f]369                                noStorageClasses,
[bd4d011]370                                LinkageSpec::Cforall,
371                                main_type,
372                                nullptr
373                        );
[2db79e5]374                        fixupGenerics(main_type, decl);
[bd4d011]375                }
376
[2065609]377                declsToAddBefore.push_back( forward );
378                if( needs_main ) declsToAddBefore.push_back( main_decl );
379                declsToAddBefore.push_back( get_decl );
[bcda04c]380
381                return get_decl;
382        }
383
384        ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) {
385                ObjectDecl * field = new ObjectDecl(
386                        field_name,
[ba3706f]387                        noStorageClasses,
[bcda04c]388                        LinkageSpec::Cforall,
389                        nullptr,
390                        new StructInstType(
391                                noQualifiers,
392                                type_decl
393                        ),
394                        nullptr
395                );
396
397                decl->get_members().push_back( field );
398
399                return field;
400        }
401
[2f9a722]402        void ConcurrentSueKeyword::addRoutines( ObjectDecl * field, FunctionDecl * func ) {
[ba3706f]403                CompoundStmt * statement = new CompoundStmt();
[bf2438c]404                statement->push_back(
[b32ada31]405                        new ReturnStmt(
[e04b636]406                                new AddressExpr(
[bcda04c]407                                        new MemberExpr(
408                                                field,
[2db79e5]409                                                new CastExpr(
410                                                        new VariableExpr( func->get_functionType()->get_parameters().front() ),
411                                                        func->get_functionType()->get_parameters().front()->get_type()->stripReferences()->clone()
412                                                )
[e04b636]413                                        )
[b32ada31]414                                )
415                        )
416                );
417
[bcda04c]418                FunctionDecl * get_decl = func->clone();
419
420                get_decl->set_statements( statement );
[e04b636]421
422                declsToAddAfter.push_back( get_decl );
423
[bcda04c]424                // get_decl->fixUniqueId();
[b32ada31]425        }
426
[64adb03]427        //=============================================================================================
428        // Mutex keyword implementation
429        //=============================================================================================
[97e3296]430
[2065609]431        void MutexKeyword::postvisit(FunctionDecl* decl) {
[102a58b]432
[db4d8e3]433                bool first = false;
434                std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl, first );
[ceedde6]435                bool isDtor = CodeGen::isDestructor( decl->name );
[64adb03]436
[ceedde6]437                // Is this function relevant to monitors
438                if( mutexArgs.empty() ) {
439                        // If this is the destructor for a monitor it must be mutex
440                        if(isDtor) {
441                                Type* ty = decl->get_functionType()->get_parameters().front()->get_type();
442
443                                // If it's a copy, it's not a mutex
444                                ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
445                                if( ! rty ) return;
446
447                                // If we are not pointing directly to a type, it's not a mutex
448                                Type* base = rty->get_base();
449                                if( dynamic_cast< ReferenceType * >( base ) ) return;
450                                if( dynamic_cast< PointerType * >( base ) ) return;
451
452                                // Check if its a struct
453                                StructInstType * baseStruct = dynamic_cast< StructInstType * >( base );
454                                if( !baseStruct ) return;
455
456                                // Check if its a monitor
457                                if(baseStruct->baseStruct->is_monitor() || baseStruct->baseStruct->is_thread())
458                                        SemanticError( decl, "destructors for structures declared as \"monitor\" must use mutex parameters\n" );
459                        }
460                        return;
461                }
[549c006]462
[ceedde6]463                // Monitors can't be constructed with mutual exclusion
464                if( CodeGen::isConstructor(decl->name) && !first ) SemanticError( decl, "constructors cannot have mutex parameters" );
[549c006]465
[ceedde6]466                // It makes no sense to have multiple mutex parameters for the destructor
[a16764a6]467                if( isDtor && mutexArgs.size() != 1 ) SemanticError( decl, "destructors can only have 1 mutex argument" );
[549c006]468
[ceedde6]469                // Make sure all the mutex arguments are monitors
[64adb03]470                for(auto arg : mutexArgs) {
471                        validate( arg );
472                }
473
[ceedde6]474                // Check if we need to instrument the body
[64adb03]475                CompoundStmt* body = decl->get_statements();
476                if( ! body ) return;
477
[ceedde6]478                // Do we have the required headers
[a16764a6]479                if( !monitor_decl || !guard_decl || !dtor_guard_decl )
[ceedde6]480                        SemanticError( decl, "mutex keyword requires monitors to be in scope, add #include <monitor>\n" );
[b32ada31]481
[ceedde6]482                // Instrument the body
[549c006]483                if( isDtor ) {
484                        addDtorStatments( decl, body, mutexArgs );
485                }
486                else {
487                        addStatments( decl, body, mutexArgs );
488                }
[64adb03]489        }
490
[2065609]491        void MutexKeyword::postvisit(StructDecl* decl) {
[102a58b]492
[9f5ecf5]493                if( decl->name == "monitor_desc" ) {
[9243cc91]494                        assert( !monitor_decl );
495                        monitor_decl = decl;
496                }
[9f5ecf5]497                else if( decl->name == "monitor_guard_t" ) {
[ef42b143]498                        assert( !guard_decl );
499                        guard_decl = decl;
500                }
[549c006]501                else if( decl->name == "monitor_dtor_guard_t" ) {
502                        assert( !dtor_guard_decl );
503                        dtor_guard_decl = decl;
504                }
[9243cc91]505        }
506
[db4d8e3]507        std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl, bool & first ) {
[64adb03]508                std::list<DeclarationWithType*> mutexArgs;
509
[db4d8e3]510                bool once = true;
[64adb03]511                for( auto arg : decl->get_functionType()->get_parameters()) {
512                        //Find mutex arguments
513                        Type* ty = arg->get_type();
[615a096]514                        if( ! ty->get_mutex() ) continue;
[64adb03]515
[db4d8e3]516                        if(once) {first = true;}
517                        once = false;
518
[64adb03]519                        //Append it to the list
520                        mutexArgs.push_back( arg );
521                }
522
523                return mutexArgs;
524        }
525
526        void MutexKeyword::validate( DeclarationWithType * arg ) {
527                Type* ty = arg->get_type();
528
529                //Makes sure it's not a copy
[870d1f0]530                ReferenceType* rty = dynamic_cast< ReferenceType * >( ty );
[a16764a6]531                if( ! rty ) SemanticError( arg, "Mutex argument must be of reference type " );
[64adb03]532
533                //Make sure the we are pointing directly to a type
[83a071f9]534                Type* base = rty->get_base();
[a16764a6]535                if( dynamic_cast< ReferenceType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " );
536                if( dynamic_cast< PointerType * >( base ) ) SemanticError( arg, "Mutex argument have exactly one level of indirection " );
[64adb03]537
538                //Make sure that typed isn't mutex
[a16764a6]539                if( base->get_mutex() ) SemanticError( arg, "mutex keyword may only appear once per argument " );
[64adb03]540        }
541
[549c006]542        void MutexKeyword::addDtorStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
543                Type * arg_type = args.front()->get_type()->clone();
544                arg_type->set_mutex( false );
545
546                ObjectDecl * monitors = new ObjectDecl(
547                        "__monitor",
[ba3706f]548                        noStorageClasses,
[549c006]549                        LinkageSpec::Cforall,
550                        nullptr,
551                        new PointerType(
552                                noQualifiers,
553                                new StructInstType(
554                                        noQualifiers,
555                                        monitor_decl
556                                )
557                        ),
558                        new SingleInit( new UntypedExpr(
559                                new NameExpr( "get_monitor" ),
560                                {  new CastExpr( new VariableExpr( args.front() ), arg_type ) }
561                        ))
562                );
563
564                assert(generic_func);
565
566                //in reverse order :
567                // monitor_guard_t __guard = { __monitors, #, func };
568                body->push_front(
[ba3706f]569                        new DeclStmt( new ObjectDecl(
[549c006]570                                "__guard",
[ba3706f]571                                noStorageClasses,
[549c006]572                                LinkageSpec::Cforall,
573                                nullptr,
574                                new StructInstType(
575                                        noQualifiers,
576                                        dtor_guard_decl
577                                ),
578                                new ListInit(
579                                        {
580                                                new SingleInit( new AddressExpr( new VariableExpr( monitors ) ) ),
581                                                new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) )
582                                        },
583                                        noDesignators,
584                                        true
585                                )
586                        ))
587                );
588
589                //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
[ba3706f]590                body->push_front( new DeclStmt( monitors) );
[549c006]591        }
592
[97e3296]593        void MutexKeyword::addStatments( FunctionDecl* func, CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
[9243cc91]594                ObjectDecl * monitors = new ObjectDecl(
595                        "__monitors",
[ba3706f]596                        noStorageClasses,
[9243cc91]597                        LinkageSpec::Cforall,
598                        nullptr,
599                        new ArrayType(
600                                noQualifiers,
601                                new PointerType(
602                                        noQualifiers,
603                                        new StructInstType(
604                                                noQualifiers,
605                                                monitor_decl
606                                        )
607                                ),
608                                new ConstantExpr( Constant::from_ulong( args.size() ) ),
609                                false,
610                                false
611                        ),
612                        new ListInit(
[bd41764]613                                map_range < std::list<Initializer*> > ( args, [](DeclarationWithType * var ){
[cb0e6de]614                                        Type * type = var->get_type()->clone();
615                                        type->set_mutex( false );
[9243cc91]616                                        return new SingleInit( new UntypedExpr(
617                                                new NameExpr( "get_monitor" ),
[cb0e6de]618                                                {  new CastExpr( new VariableExpr( var ), type ) }
[9243cc91]619                                        ) );
620                                })
621                        )
622                );
623
[97e3296]624                assert(generic_func);
625
[64adb03]626                //in reverse order :
[97e3296]627                // monitor_guard_t __guard = { __monitors, #, func };
[64adb03]628                body->push_front(
[ba3706f]629                        new DeclStmt( new ObjectDecl(
[64adb03]630                                "__guard",
[ba3706f]631                                noStorageClasses,
[64adb03]632                                LinkageSpec::Cforall,
633                                nullptr,
634                                new StructInstType(
635                                        noQualifiers,
[ef42b143]636                                        guard_decl
[64adb03]637                                ),
638                                new ListInit(
639                                        {
[9243cc91]640                                                new SingleInit( new VariableExpr( monitors ) ),
[97e3296]641                                                new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) ),
642                                                new SingleInit( new CastExpr( new VariableExpr( func ), generic_func->clone() ) )
[ef42b143]643                                        },
644                                        noDesignators,
645                                        true
[64adb03]646                                )
647                        ))
648                );
649
[ef42b143]650                //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
[ba3706f]651                body->push_front( new DeclStmt( monitors) );
[64adb03]652        }
[bd4d011]653
654        //=============================================================================================
655        // General entry routine
656        //=============================================================================================
[549c006]657        void ThreadStarter::previsit( StructDecl * decl ) {
658                if( decl->name == "thread_desc" && decl->body ) {
659                        assert( !thread_decl );
660                        thread_decl = decl;
661                }
662        }
663
[2065609]664        void ThreadStarter::postvisit(FunctionDecl * decl) {
[9f5ecf5]665                if( ! CodeGen::isConstructor(decl->name) ) return;
[bd4d011]666
[549c006]667                Type * typeof_this = InitTweak::getTypeofThis(decl->type);
668                StructInstType * ctored_type = dynamic_cast< StructInstType * >( typeof_this );
669                if( ctored_type && ctored_type->baseStruct == thread_decl ) {
670                        thread_ctor_seen = true;
671                }
672
[bd4d011]673                DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
[ce8c12f]674                auto type  = dynamic_cast< StructInstType * >( InitTweak::getPointerBase( param->get_type() ) );
[bd4d011]675                if( type && type->get_baseStruct()->is_thread() ) {
[549c006]676                        if( !thread_decl || !thread_ctor_seen ) {
[a16764a6]677                                SemanticError( type->get_baseStruct()->location, "thread keyword requires threads to be in scope, add #include <thread>");
[549c006]678                        }
679
[bd4d011]680                        addStartStatement( decl, param );
681                }
682        }
683
684        void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) {
685                CompoundStmt * stmt = decl->get_statements();
686
687                if( ! stmt ) return;
688
[bf2438c]689                stmt->push_back(
[bd4d011]690                        new ExprStmt(
691                                new UntypedExpr(
692                                        new NameExpr( "__thrd_start" ),
693                                        { new VariableExpr( param ) }
694                                )
695                        )
696                );
697        }
[68fe077a]698};
[6b0b624]699
700// Local Variables: //
701// mode: c //
702// tab-width: 4 //
703// End: //
Note: See TracBrowser for help on using the repository browser.