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