Changeset bd4d011 for src/Concurrency


Ignore:
Timestamp:
Mar 23, 2017, 3:05:36 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
ae6f1ec
Parents:
bcda04c
Message:

Implemented thread keyword

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Concurrency/Keywords.cc

    rbcda04c rbd4d011  
    1717#include "Concurrency/Keywords.h"
    1818
     19#include "InitTweak/InitTweak.h"
    1920#include "SymTab/AddVisit.h"
    2021#include "SynTree/Declaration.h"
     
    5354          public:
    5455
    55                 ConcurrentSueKeyword( std::string&& type_name, std::string&& field_name, std::string&& getter_name, std::string&& context_error ) :
    56                   type_name( type_name ), field_name( field_name ), getter_name( getter_name ), context_error( context_error ) {}
     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 ) {}
    5758
    5859                virtual ~ConcurrentSueKeyword() {}
     
    7374                const std::string getter_name;
    7475                const std::string context_error;
     76                bool needs_main;
    7577
    7678                std::list< Declaration * > declsToAdd, declsToAddAfter;
     
    9597                        "__thrd",
    9698                        "get_thread",
    97                         "thread keyword requires threads to be in scope, add #include <thread>"
     99                        "thread keyword requires threads to be in scope, add #include <thread>",
     100                        true
    98101                )
    99102                {}
     
    125128                        "__cor",
    126129                        "get_coroutine",
    127                         "coroutine keyword requires coroutines to be in scope, add #include <coroutine>"
     130                        "coroutine keyword requires coroutines to be in scope, add #include <coroutine>",
     131                        true
    128132                )
    129133                {}
     
    155159                        "__mon",
    156160                        "get_monitor",
    157                         "monitor keyword requires monitors to be in scope, add #include <monitor>"
     161                        "monitor keyword requires monitors to be in scope, add #include <monitor>",
     162                        false
    158163                )
    159164                {}
     
    198203        };
    199204
     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
    200227        //=============================================================================================
    201228        // General entry routine
     
    212239
    213240        void implementThreadStarter( std::list< Declaration * > & translationUnit ) {
    214 
     241                ThreadStarter   ::implement( translationUnit );
    215242        }
    216243
     
    246273                forward->get_members().clear();
    247274
    248                 FunctionType * type = new FunctionType( noQualifiers, false );
     275                FunctionType * get_type = new FunctionType( noQualifiers, false );
    249276                ObjectDecl * this_decl = new ObjectDecl(
    250277                        "this",
     
    262289                );
    263290
    264                 type->get_parameters().push_back( this_decl );
    265                 type->get_returnVals().push_back(
     291                get_type->get_parameters().push_back( this_decl );
     292                get_type->get_returnVals().push_back(
    266293                        new ObjectDecl(
    267294                                "ret",
     
    284311                        Type::Static,
    285312                        LinkageSpec::Cforall,
    286                         type,
     313                        get_type,
    287314                        nullptr,
    288315                        noAttributes,
     
    290317                );
    291318
     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
    292335                declsToAdd.push_back( forward );
     336                if( needs_main ) declsToAdd.push_back( main_decl );
    293337                declsToAdd.push_back( get_decl );
    294338
     
    455499                body->push_front( new DeclStmt( noLabels, monitors) );
    456500        }
     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        }
    457533};
Note: See TracChangeset for help on using the changeset viewer.