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