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