source: src/Concurrency/Keywords.cc @ 70ead46a

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 70ead46a was ac2b598, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Changed descriptors for concurrency to use $ prefix instead of trailing _desc

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