source: src/Concurrency/Keywords.cc @ 3fb7f5e

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 3fb7f5e was bd4d011, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Implemented thread keyword

  • Property mode set to 100644
File size: 16.4 KB
RevLine 
[64adb03]1//                              -*- Mode: CPP -*-
2//
3// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4//
5// The contents of this file are covered under the licence agreement in the
6// file "LICENCE" distributed with Cforall.
7//
8// Keywords.cc --
9//
10// Author           : Thierry Delisle
11// Created On       : Mon Mar 13 12:41:22 2017
12// Last Modified By :
13// Last Modified On :
[615a096]14// Update Count     : 3
[64adb03]15//
16
17#include "Concurrency/Keywords.h"
18
[bd4d011]19#include "InitTweak/InitTweak.h"
[b32ada31]20#include "SymTab/AddVisit.h"
[64adb03]21#include "SynTree/Declaration.h"
22#include "SynTree/Expression.h"
23#include "SynTree/Initializer.h"
24#include "SynTree/Statement.h"
25#include "SynTree/Type.h"
26#include "SynTree/Visitor.h"
27
28namespace Concurrency {
29
30        namespace {
31                const std::list<Label> noLabels;
[b32ada31]32                const std::list< Attribute * > noAttributes;
[68fe077a]33                Type::StorageClasses noStorage;
[64adb03]34                Type::Qualifiers noQualifiers;
35        }
36
37        //=============================================================================================
38        // Visitors declaration
39        //=============================================================================================
40
[bcda04c]41        //-----------------------------------------------------------------------------
42        //Handles sue type declarations :
43        // sue MyType {                             struct MyType {
44        //      int data;                                  int data;
45        //      a_struct_t more_data;                      a_struct_t more_data;
46        //                                =>             NewField_t newField;
47        // };                                        };
48        //                                           static inline NewField_t * getter_name( MyType * this ) { return &this->newField; }
49        //
50        class ConcurrentSueKeyword : public Visitor {
51          protected:
52            template< typename Visitor >
53            friend void SymTab::acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor );
54          public:
55
[bd4d011]56                ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error, bool needs_main ) :
57                  type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ), needs_main( needs_main ) {}
[bcda04c]58
59                virtual ~ConcurrentSueKeyword() {}
60
61                using Visitor::visit;
62                virtual void visit( StructDecl * decl ) override final;
63
64                void handle( StructDecl * );
65                FunctionDecl * forwardDeclare( StructDecl * );
66                ObjectDecl * addField( StructDecl * );
67                void addRoutines( StructDecl *, ObjectDecl *, FunctionDecl * );
68
69                virtual bool is_target( StructDecl * decl ) = 0;
70
71          private:
72                const std::string type_name;
73                const std::string field_name;
74                const std::string getter_name;
75                const std::string context_error;
[bd4d011]76                bool needs_main;
[bcda04c]77
78                std::list< Declaration * > declsToAdd, declsToAddAfter;
79                StructDecl* type_decl = nullptr;
80        };
81
82
[64adb03]83        //-----------------------------------------------------------------------------
84        //Handles thread type declarations :
85        // thread Mythread {                         struct MyThread {
86        //      int data;                                  int data;
87        //      a_struct_t more_data;                      a_struct_t more_data;
88        //                                =>             thread_desc __thrd_d;
89        // };                                        };
90        //                                           static inline thread_desc * get_thread( MyThread * this ) { return &this->__thrd_d; }
91        //
[bcda04c]92        class ThreadKeyword final : public ConcurrentSueKeyword {
[64adb03]93          public:
94
[bcda04c]95                ThreadKeyword() : ConcurrentSueKeyword(
96                        "thread_desc",
97                        "__thrd",
98                        "get_thread",
[bd4d011]99                        "thread keyword requires threads to be in scope, add #include <thread>",
100                        true
[bcda04c]101                )
102                {}
103
104                virtual ~ThreadKeyword() {}
105
106                virtual bool is_target( StructDecl * decl ) override final { return decl->is_thread(); }
107
108                static void implement( std::list< Declaration * > & translationUnit ) {
109                        ThreadKeyword impl;
110                        SymTab::acceptAndAdd( translationUnit, impl );
111                }
[64adb03]112        };
113
114        //-----------------------------------------------------------------------------
115        //Handles coroutine type declarations :
116        // coroutine MyCoroutine {                   struct MyCoroutine {
117        //      int data;                                  int data;
118        //      a_struct_t more_data;                      a_struct_t more_data;
119        //                                =>             coroutine_desc __cor_d;
120        // };                                        };
121        //                                           static inline coroutine_desc * get_coroutine( MyCoroutine * this ) { return &this->__cor_d; }
122        //
[bcda04c]123        class CoroutineKeyword final : public ConcurrentSueKeyword {
[64adb03]124          public:
125
[bcda04c]126                CoroutineKeyword() : ConcurrentSueKeyword(
127                        "coroutine_desc",
128                        "__cor",
129                        "get_coroutine",
[bd4d011]130                        "coroutine keyword requires coroutines to be in scope, add #include <coroutine>",
131                        true
[bcda04c]132                )
133                {}
[b32ada31]134
[bcda04c]135                virtual ~CoroutineKeyword() {}
136
137                virtual bool is_target( StructDecl * decl ) override final { return decl->is_coroutine(); }
[b32ada31]138
139                static void implement( std::list< Declaration * > & translationUnit ) {
140                        CoroutineKeyword impl;
141                        SymTab::acceptAndAdd( translationUnit, impl );
142                }
[64adb03]143        };
144
145        //-----------------------------------------------------------------------------
146        //Handles monitor type declarations :
147        // monitor MyMonitor {                       struct MyMonitor {
148        //      int data;                                  int data;
149        //      a_struct_t more_data;                      a_struct_t more_data;
150        //                                =>             monitor_desc __mon_d;
151        // };                                        };
152        //                                           static inline monitor_desc * get_coroutine( MyMonitor * this ) { return &this->__cor_d; }
153        //
[bcda04c]154        class MonitorKeyword final : public ConcurrentSueKeyword {
[64adb03]155          public:
156
[bcda04c]157                MonitorKeyword() : ConcurrentSueKeyword(
158                        "monitor_desc",
159                        "__mon",
160                        "get_monitor",
[bd4d011]161                        "monitor keyword requires monitors to be in scope, add #include <monitor>",
162                        false
[bcda04c]163                )
164                {}
165
166                virtual ~MonitorKeyword() {}
167
168                virtual bool is_target( StructDecl * decl ) override final { return decl->is_monitor(); }
169
170                static void implement( std::list< Declaration * > & translationUnit ) {
171                        MonitorKeyword impl;
172                        SymTab::acceptAndAdd( translationUnit, impl );
173                }
[64adb03]174        };
175
176        //-----------------------------------------------------------------------------
177        //Handles mutex routines definitions :
178        // void foo( A * mutex a, B * mutex b,  int i ) {                  void foo( A * a, B * b,  int i ) {
[9243cc91]179        //                                                                       monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
[64adb03]180        //                                                                       monitor_guard_t __guard = { __monitors, 2 };
181        //    /*Some code*/                                       =>           /*Some code*/
182        // }                                                               }
183        //
184        class MutexKeyword final : public Visitor {
185          public:
186
187                using Visitor::visit;
[b32ada31]188                virtual void visit( FunctionDecl * decl ) override final;
189                virtual void visit(   StructDecl * decl ) override final;
[64adb03]190
191                std::list<DeclarationWithType*> findMutexArgs( FunctionDecl* );
192                void validate( DeclarationWithType * );
193                void addStatments( CompoundStmt *, const std::list<DeclarationWithType * > &);
194
195                static void implement( std::list< Declaration * > & translationUnit ) {
196                        MutexKeyword impl;
197                        acceptAll( translationUnit, impl );
198                }
[9243cc91]199
200          private:
201                StructDecl* monitor_decl = nullptr;
[ef42b143]202                StructDecl* guard_decl = nullptr;
[64adb03]203        };
204
[bd4d011]205        //-----------------------------------------------------------------------------
206        //Handles mutex routines definitions :
207        // void foo( A * mutex a, B * mutex b,  int i ) {                  void foo( A * a, B * b,  int i ) {
208        //                                                                       monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
209        //                                                                       monitor_guard_t __guard = { __monitors, 2 };
210        //    /*Some code*/                                       =>           /*Some code*/
211        // }                                                               }
212        //
213        class ThreadStarter final : public Visitor {
214          public:
215
216                using Visitor::visit;
217                virtual void visit( FunctionDecl * decl ) override final;
218
219                void addStartStatement( FunctionDecl * decl, DeclarationWithType * param );
220
221                static void implement( std::list< Declaration * > & translationUnit ) {
222                        ThreadStarter impl;
223                        acceptAll( translationUnit, impl );
224                }
225        };
226
[64adb03]227        //=============================================================================================
228        // General entry routine
229        //=============================================================================================
230        void applyKeywords( std::list< Declaration * > & translationUnit ) {
231                ThreadKeyword   ::implement( translationUnit );
232                CoroutineKeyword        ::implement( translationUnit );
233                MonitorKeyword  ::implement( translationUnit );
[bcda04c]234        }
235
236        void implementMutexFuncs( std::list< Declaration * > & translationUnit ) {
[64adb03]237                MutexKeyword    ::implement( translationUnit );
238        }
239
[bcda04c]240        void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
[bd4d011]241                ThreadStarter   ::implement( translationUnit );
[bcda04c]242        }
243
[b32ada31]244        //=============================================================================================
[bcda04c]245        // Generic keyword implementation
[b32ada31]246        //=============================================================================================
[bcda04c]247        void ConcurrentSueKeyword::visit(StructDecl * decl) {
248                if( decl->get_name() == type_name ) {
249                        assert( !type_decl );
250                        type_decl = decl;
[b32ada31]251                }
[bcda04c]252                else if ( is_target(decl) ) {
[b32ada31]253                        handle( decl );
254                }
255
256        }
257
[bcda04c]258        void ConcurrentSueKeyword::handle( StructDecl * decl ) {
[b32ada31]259                if( ! decl->has_body() ) return;
260
[bcda04c]261                if( !type_decl ) throw SemanticError( context_error, decl );
[b32ada31]262
[bcda04c]263                FunctionDecl * func = forwardDeclare( decl );
264                ObjectDecl * field = addField( decl );
265                addRoutines( decl, field, func );
[b32ada31]266        }
267
[bcda04c]268        FunctionDecl * ConcurrentSueKeyword::forwardDeclare( StructDecl * decl ) {
269
270                StructDecl * forward = decl->clone();
271                forward->set_body( false );
272                deleteAll( forward->get_members() );
273                forward->get_members().clear();
274
[bd4d011]275                FunctionType * get_type = new FunctionType( noQualifiers, false );
[bcda04c]276                ObjectDecl * this_decl = new ObjectDecl(
277                        "this",
[b32ada31]278                        noStorage,
279                        LinkageSpec::Cforall,
280                        nullptr,
[bcda04c]281                        new PointerType(
[b32ada31]282                                noQualifiers,
[bcda04c]283                                new StructInstType(
284                                        noQualifiers,
285                                        decl
286                                )
[b32ada31]287                        ),
288                        nullptr
289                );
290
[bd4d011]291                get_type->get_parameters().push_back( this_decl );
292                get_type->get_returnVals().push_back(
[e04b636]293                        new ObjectDecl(
294                                "ret",
295                                noStorage,
296                                LinkageSpec::Cforall,
297                                nullptr,
298                                new PointerType(
299                                        noQualifiers,
300                                        new StructInstType(
301                                                noQualifiers,
[bcda04c]302                                                type_decl
[e04b636]303                                        )
304                                ),
305                                nullptr
306                        )
307                );
[b32ada31]308
[bcda04c]309                FunctionDecl * get_decl = new FunctionDecl(
310                        getter_name,
311                        Type::Static,
312                        LinkageSpec::Cforall,
[bd4d011]313                        get_type,
[bcda04c]314                        nullptr,
315                        noAttributes,
316                        Type::Inline
317                );
318
[bd4d011]319                FunctionDecl * main_decl = nullptr;
320
321                if( needs_main ) {
322                        FunctionType * main_type = new FunctionType( noQualifiers, false );
323                       
324                        main_type->get_parameters().push_back( this_decl->clone() );
325
326                        main_decl = new FunctionDecl(
327                                "main",
328                                noStorage,
329                                LinkageSpec::Cforall,
330                                main_type,
331                                nullptr
332                        );
333                }
334
[bcda04c]335                declsToAdd.push_back( forward );
[bd4d011]336                if( needs_main ) declsToAdd.push_back( main_decl );
[bcda04c]337                declsToAdd.push_back( get_decl );
338
339                return get_decl;
340        }
341
342        ObjectDecl * ConcurrentSueKeyword::addField( StructDecl * decl ) {
343                ObjectDecl * field = new ObjectDecl(
344                        field_name,
345                        noStorage,
346                        LinkageSpec::Cforall,
347                        nullptr,
348                        new StructInstType(
349                                noQualifiers,
350                                type_decl
351                        ),
352                        nullptr
353                );
354
355                decl->get_members().push_back( field );
356
357                return field;
358        }
359
360        void ConcurrentSueKeyword::addRoutines( StructDecl * decl, ObjectDecl * field, FunctionDecl * func ) {
[b32ada31]361                CompoundStmt * statement = new CompoundStmt( noLabels );
362                statement->push_back( 
363                        new ReturnStmt(
364                                noLabels,
[e04b636]365                                new AddressExpr(
[bcda04c]366                                        new MemberExpr(
367                                                field,
368                                                UntypedExpr::createDeref( new VariableExpr( func->get_functionType()->get_parameters().front() ) )
[e04b636]369                                        )
[b32ada31]370                                )
371                        )
372                );
373
[bcda04c]374                FunctionDecl * get_decl = func->clone();
375
376                get_decl->set_statements( statement );
[e04b636]377
378                declsToAddAfter.push_back( get_decl );
379
[bcda04c]380                // get_decl->fixUniqueId();
[b32ada31]381        }
382
[64adb03]383        //=============================================================================================
384        // Mutex keyword implementation
385        //=============================================================================================
386        void MutexKeyword::visit(FunctionDecl* decl) {
387                std::list<DeclarationWithType*> mutexArgs = findMutexArgs( decl );
388                if( mutexArgs.empty() ) return;
389
390                for(auto arg : mutexArgs) {
391                        validate( arg );
392                }
393
394                CompoundStmt* body = decl->get_statements();
395                if( ! body ) return;
396
[b32ada31]397                if( !monitor_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
398                if( !guard_decl ) throw SemanticError( "mutex keyword requires monitors to be in scope, add #include <monitor>", decl );
399
[64adb03]400                addStatments( body, mutexArgs );
401        }
402
[9243cc91]403        void MutexKeyword::visit(StructDecl* decl) {
404                if( decl->get_name() == "monitor_desc" ) {
405                        assert( !monitor_decl );
406                        monitor_decl = decl;
407                }
[ef42b143]408                else if( decl->get_name() == "monitor_guard_t" ) {
409                        assert( !guard_decl );
410                        guard_decl = decl;
411                }
[9243cc91]412        }
413
[64adb03]414        std::list<DeclarationWithType*> MutexKeyword::findMutexArgs( FunctionDecl* decl ) {
415                std::list<DeclarationWithType*> mutexArgs;
416
417                for( auto arg : decl->get_functionType()->get_parameters()) {
418                        //Find mutex arguments
419                        Type* ty = arg->get_type();
[615a096]420                        if( ! ty->get_mutex() ) continue;
[64adb03]421
422                        //Append it to the list
423                        mutexArgs.push_back( arg );
424                }
425
426                return mutexArgs;
427        }
428
429        void MutexKeyword::validate( DeclarationWithType * arg ) {
430                Type* ty = arg->get_type();
431
432                //Makes sure it's not a copy
433                PointerType* pty = dynamic_cast< PointerType * >( ty );
434                if( ! pty ) throw SemanticError( "Mutex argument must be of pointer/reference type ", arg );
435
436                //Make sure the we are pointing directly to a type
437                Type* base = pty->get_base();
438                if(  dynamic_cast< PointerType * >( base ) ) throw SemanticError( "Mutex argument have exactly one level of indirection ", arg );
439
440                //Make sure that typed isn't mutex
[e04b636]441                if( base->get_mutex() ) throw SemanticError( "mutex keyword may only appear once per argument ", arg );
[64adb03]442        }
443
444        void MutexKeyword::addStatments( CompoundStmt * body, const std::list<DeclarationWithType * > & args ) {
[9243cc91]445                ObjectDecl * monitors = new ObjectDecl(
446                        "__monitors",
447                        noStorage,
448                        LinkageSpec::Cforall,
449                        nullptr,
450                        new ArrayType(
451                                noQualifiers,
452                                new PointerType(
453                                        noQualifiers,
454                                        new StructInstType(
455                                                noQualifiers,
456                                                monitor_decl
457                                        )
458                                ),
459                                new ConstantExpr( Constant::from_ulong( args.size() ) ),
460                                false,
461                                false
462                        ),
463                        new ListInit(
[cb0e6de]464                                map_range < std::list<Initializer*> > ( args, [this](DeclarationWithType * var ){
465                                        Type * type = var->get_type()->clone();
466                                        type->set_mutex( false );
[9243cc91]467                                        return new SingleInit( new UntypedExpr(
468                                                new NameExpr( "get_monitor" ),
[cb0e6de]469                                                {  new CastExpr( new VariableExpr( var ), type ) }
[9243cc91]470                                        ) );
471                                })
472                        )
473                );
474
[64adb03]475                //in reverse order :
476                // monitor_guard_t __guard = { __monitors, # };
477                body->push_front(
478                        new DeclStmt( noLabels, new ObjectDecl(
479                                "__guard",
480                                noStorage,
481                                LinkageSpec::Cforall,
482                                nullptr,
483                                new StructInstType(
484                                        noQualifiers,
[ef42b143]485                                        guard_decl
[64adb03]486                                ),
487                                new ListInit(
488                                        {
[9243cc91]489                                                new SingleInit( new VariableExpr( monitors ) ),
[64adb03]490                                                new SingleInit( new ConstantExpr( Constant::from_ulong( args.size() ) ) )
[ef42b143]491                                        },
492                                        noDesignators,
493                                        true
[64adb03]494                                )
495                        ))
496                );
497
[ef42b143]498                //monitor_desc * __monitors[] = { get_monitor(a), get_monitor(b) };
[9243cc91]499                body->push_front( new DeclStmt( noLabels, monitors) );
[64adb03]500        }
[bd4d011]501
502        //=============================================================================================
503        // General entry routine
504        //=============================================================================================
505        void ThreadStarter::visit(FunctionDecl * decl) {
506                if( ! InitTweak::isConstructor(decl->get_name()) ) return;
507
508                DeclarationWithType * param = decl->get_functionType()->get_parameters().front();
509                auto ptr = dynamic_cast< PointerType * >( param->get_type() );
510                // if( ptr ) std::cerr << "FRED1" << std::endl;
511                auto type  = dynamic_cast< StructInstType * >( ptr->get_base() );
512                // if( type ) std::cerr << "FRED2" << std::endl;
513                if( type && type->get_baseStruct()->is_thread() ) {
514                        addStartStatement( decl, param );
515                }
516        }
517
518        void ThreadStarter::addStartStatement( FunctionDecl * decl, DeclarationWithType * param ) {
519                CompoundStmt * stmt = decl->get_statements();
520
521                if( ! stmt ) return;
522
523                stmt->push_back( 
524                        new ExprStmt(
525                                noLabels,
526                                new UntypedExpr(
527                                        new NameExpr( "__thrd_start" ),
528                                        { new VariableExpr( param ) }
529                                )
530                        )
531                );
532        }
[68fe077a]533};
Note: See TracBrowser for help on using the repository browser.